GSG-G7/shrinc-v2

GraphQL

Closed this issue · 0 comments

GraphQL

  • set up
  • graphql route
  • schema

set up

npm i graphql express-graghql

graphql route

app.use('/graphql',graphqlHttp({
schema,
graphiql:true})

schema

create GraphQL schema which contains type and Root query and mutation.
type:

const therapistType = new GraphQLObjectType({
  name: 'Therapist',
  fields: () => ({
    id: { type: GraphQLString },
    fullName: { type:GraphQLString },
    email: { type:GraphQLString },
    password: { type: GraphQLString },
    city: { type: GraphQLString },
    postCode: { type:GraphQLString },
    type: { type:GraphQLString },
    priceRange: { type:GraphQLString },
    language: { type: new GraphQLList(GraphQLString) },
    insurance: { type: new GraphQLList(GraphQLString) },
    approch: { type:GraphQLString },
    avalibility: { type:GraphQLString },
    image: { type:GraphQLString },
    remote: { type:GraphQLBoolean },
    skype: { type:GraphQLString },
  })
});

query:

const RootQuery = new GraphQLObjectType({
  name: 'therapistQuery',
  fields: {
    therapists: {
      type: therapistType,
      resolve(parent, args) {
         // return Therapists
        }
      }
  }
})

mutation:

const Mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields:{
    addTherapist:{
      type: therapistType,
      args:{
    id: { type: GraphQLString },
    fullName: { type:GraphQLString },
    email: { type:GraphQLString },
    password: { type: GraphQLString },
    city: { type: GraphQLString },
    postCode: { type:GraphQLString },
    type: { type:GraphQLString },
    priceRange: { type:GraphQLString },
    language: { type: new GraphQLList(GraphQLString) },
    insurance: { type: new GraphQLList(GraphQLString) },
    approch: { type:GraphQLString },
    avalibility: { type:GraphQLString },
    image: { type:GraphQLString },
    remote: { type:GraphQLBoolean },
    skype: { type:GraphQLString },
      },
      resolve(parent,args){
        // make some functions(password hashing, host image by Cloudinary) on data 
       //  save data (therapist account) in MongoDB
      }
    }
  }
})