Modularization of the schema using AdonisJs and GraphQL

5

I have a project where I'm using AdonisJS and GraphQL, I've created the schema and defined the queries , mutations , types . But everything was centralized in just one file, so I decided to modularize schema and resolvers .

The problem is that when I export the files I modularized, I get the following error:

typeError: Cannot read property 'kind' of undefined
at D:\Desenvolvimento\statsway\adonis-graphql-server\app\data\schema.js(anonymous):30
25    type Schema {
26      query: Query
27      mutation: Mutation
28    }
30  module.exports = makeExecutableSchema({
31    typeDefs: [
32      typeDefs,
33      Query,
34      Mutation,
35      userTypes

The question is: can not I concatenate this way within module.exports ? Or would it be some problem with imports ?

Here is a complete example:

UserSchema:

const userTypes = '
    # User definition type
    type User {
        id: Int!
        username: String!
        email: String!
        posts: [Post]
    }
';

const userQueries = '
    allUsers: [User]
    fetchUser(id: Int!): User
';

const userMutations = '
    login (email: String!, password: String!): String
    createUser (username: String!, email: String!, password: String!): User
';

module.exports = {
  userTypes,
  userQueries,
  userMutations
}

UserResolver:

'use strict'
const User = use('App/Models/User')

const userResolver = {
    Query: {
        async allUsers() {
          const users = await User.all()
          return users.toJSON()
        },
        async fetchUser(_, { id }) {
          const user = await User.find(id)
          return user.toJSON()
        }
    },
    Mutation: {
        async login(_, { email, password }, { auth }) {
          const { token } = await auth.attempt(email, password)
          return token
        },

        async createUser(_, { username, email, password }) {
          return await User.create({ username, email, password })
        },
    },
    User: {
        async posts(userInJson) {
          const user = new User()
          user.newUp(userInJson)

          const posts = await user.posts().fetch()
          return posts.toJSON()
        }
    }
}
module.exports = userResolver;

Mutation:

const { userMutations } = require('./user/userSchema');

const Mutation = '
    type Mutation {
        ${userMutations}
    }
';

module.exports = Mutation

Query:

const { userQueries } = require('./user/userSchema');

const Query = '
    type Query {
        ${userQueries}
    }
';

module.exports = Query

And finally the file where the error appears to occur, Schema :

'use strict'

const { makeExecutableSchema } = require('graphql-tools')
const { Query } = require ('./query');
const { Mutation } = require('./mutation');
const { merge } = require ('lodash');

const { userTypes } = require ('./user/userSchema');
const { userResolver } = require ('./user/userResolver');

const resolvers = merge(
  userResolver
)
// Define our schema using the GraphQL schema language
const typeDefs = '
  type Schema {
    query: Query
    mutation: Mutation
  }
'
module.exports = makeExecutableSchema({ 
  typeDefs: [
    typeDefs,
    Query,
    Mutation,
    userTypes,
], resolvers })

Any suggestions on what the problem might be?

    
asked by anonymous 22.11.2018 / 13:01

1 answer

3

I did a search and it looks like this error occurs in the package TypeScript between versions 2.4 and 2.7 (currently is in version 3.2.1), when "members of a class are transformed with decorators", see here:

  

So try to change the version of the TypeScript package (in the file package.json ) to a more recent version (the 3.2.1 is the most recent, but I do not know if AdonisJS accepts this version, I could not find this information, the Angular eg does not accept ):

"typescript": "^3.2.1",

After that do the following :

  • Delete the file package_locked.json ;
  • Delete the folder node_modules ;
  • Run the command npm install .
30.11.2018 / 17:56