implements pattern for graphql

This commit is contained in:
James
2020-04-02 21:48:38 -04:00
parent 1a3f36f258
commit d4f9793777
5 changed files with 69 additions and 21 deletions

43
src/graphql/init.js Normal file
View 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;