Schema modularization using Adonisjs and Graphql

Asked

Viewed 462 times

5

I have a project where I am using Adonisjs and Graphql, I created the schema and defined the darlings, mutations, types. But everything was centered on just one file, so I decided to modularize the schema and the resolve.

The problem is that when I export the files I modularized, I have 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 this: I can’t make the concatenation that way inside the module.exports? Or would it be some problem of imports?

The following 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 apparently the error occurs, 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 hints of what the problem might be?

1 answer

3


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

Then try changing the version of the Typescript package (in the file package.json) for a newer version (3.2.1 is the latest, but I don’t know if Adonisjs accepts this version, I couldn’t find this information, Angular for example unaccepted):

"typescript": "^3.2.1",

After that do the following:

  • Delete the file package_locked.json;
  • Delete the folder node_modules;
  • Execute the command npm install.
  • 1

    Really was the problem, helped a lot. Thanks.

  • Good that you solved Diego! And what version of Typescript did you use? (to leave documented in case someone needs the solution too)

  • 1

    I used 3.2.1 even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.