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?
Really was the problem, helped a lot. Thanks.
– DiegoAugusto
Good that you solved Diego! And what version of Typescript did you use? (to leave documented in case someone needs the solution too)
– Pedro Gaspar
I used 3.2.1 even.
– DiegoAugusto