RisingStack / graffiti-mongoose

⚠️ DEVELOPMENT DISCONTINUED - Mongoose (MongoDB) adapter for graffiti (Node.js GraphQL ORM)
https://risingstack-graffiti.signup.team/
MIT License
382 stars 52 forks source link

Custom queries/mutations #211

Closed JonathanWbn closed 7 years ago

JonathanWbn commented 7 years ago

Hey there, first of all thanks for the package, really good stuff.

I am trying to figure out how to write custom queries, but I am struggling with the syntax. At the moment I am doing something like this for my schema:

const schema = getSchema([Service], {
  customQueries: {
    testQuery: {
      type: new GraphQLObjectType({
        name: 'TestQuery',
        type: GraphQLString,
        fields: () => ({
          hello: {
            name: 'someString',
            type: GraphQLString,
            resolve: () => 'world'
          }
        })
      }),
      args: []
    }
  }
});

When I run the test query tho i just get this result:

{
  "data": {
    "testQuery": null
  }
}

Where am I going wrong here?

JohnProg commented 7 years ago

This was my solution:

// schema.js
const { GraphQLObjectType, GraphQLSchema } = require('graphql');
const { getModels } = require('@risingstack/graffiti-mongoose/lib/model');
const { getFields } = require('@risingstack/graffiti-mongoose/lib/schema');
const { contactMutation } = require('./mutations');
const { contactQuery } = require('./queries');
const models = require('../models');

const graffitiModels = getModels(models);
const graffitiFields = getFields(graffitiModels);

const rootQuery = graffitiFields.query._typeConfig;
const rootMutation = graffitiFields.mutation._typeConfig;

Object.assign(rootQuery.fields, {
  contact: contactQuery.user,
  contacts: contactQuery.users,
});

Object.assign(rootMutation.fields, {
  createContact: contactMutation.createContact,
});

module.exports = new GraphQLSchema({
  query: new GraphQLObjectType(rootQuery),
  mutation: new GraphQLObjectType(rootMutation),
});

I hope this can help you :).