implements pattern for graphql
This commit is contained in:
43
src/graphql/init.js
Normal file
43
src/graphql/init.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const graphQLHTTP = require('express-graphql');
|
||||
const { GraphQLObjectType, GraphQLString, GraphQLSchema } = require('graphql');
|
||||
|
||||
const queryType = {
|
||||
name: 'Query',
|
||||
fields: {},
|
||||
};
|
||||
|
||||
function init() {
|
||||
const userType = new GraphQLObjectType({
|
||||
name: 'User',
|
||||
fields: {
|
||||
id: { type: GraphQLString },
|
||||
email: { type: GraphQLString },
|
||||
},
|
||||
});
|
||||
|
||||
queryType.fields.user = {
|
||||
type: userType,
|
||||
args: {
|
||||
id: { type: GraphQLString },
|
||||
},
|
||||
resolve: (_, { id }) => {
|
||||
return {
|
||||
id,
|
||||
email: 'test',
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const query = new GraphQLObjectType(queryType);
|
||||
const schema = new GraphQLSchema({ query });
|
||||
|
||||
return graphQLHTTP({
|
||||
schema,
|
||||
graphiql: true,
|
||||
context: ({ req }) => ({
|
||||
user: req.user,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = init;
|
||||
Reference in New Issue
Block a user