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?