Cannot return null for non-nullable field , Debugger dosen't show null [duplicate] - graphql

This question already has answers here:
Why does a GraphQL query return null?
(6 answers)
Closed 2 years ago.
I have this schema.graphql
### This file was generated by Nexus Schema
### Do not make changes to this file directly
type AuthPayload {
token: String!
users: users!
}
scalar DateTime
type Mutation {
login(email: String, password: String): AuthPayload!
signup(CREATED_BY: String, EMAIL: String, FIRST_NAME: String, IS_ACTIVE: Boolean, PASSWORD: String, USERNAME: String): AuthPayload!
}
type Query {
me: users
}
type users {
CREATED_BY: String!
CREATED_ON: DateTime
EMAIL: String!
FIRST_NAME: String!
id: Int!
IS_ACTIVE: Boolean!
LAST_NAME: String
MODIFIED_BY: String
MODIFIED_ON: DateTime
ORGANIZATION_ID: String
PASSWORD: String!
PHONE: String
USERNAME: String!
}
mutations :-
const Mutation = mutationType({
definition(t) {
t.field('signup', {
type: 'AuthPayload',
args: {
FIRST_NAME: stringArg({ nullable: true }),
EMAIL: stringArg(),
PASSWORD: stringArg(),
IS_ACTIVE: booleanArg(),
USERNAME: stringArg(),
CREATED_BY: stringArg(),
},
resolve: async (parent, { FIRST_NAME, EMAIL, PASSWORD ,IS_ACTIVE ,USERNAME,CREATED_BY }, ctx) => {
const hashedPassword = await hash(PASSWORD, 10)
const user = await ctx.prisma.users.create({
data: {
FIRST_NAME,
EMAIL,
PASSWORD: hashedPassword,
IS_ACTIVE,
USERNAME,
CREATED_BY
},
})
return {
token: sign({ userId: user.id }, APP_SECRET),
user,
}
},
})
t.field('login', {
type: 'AuthPayload',
args: {
email: stringArg(),
password: stringArg(),
},
resolve: async (parent, { email, password }, context) => {
const user = await context.prisma.users.findOne({
where: {
EMAIL : email,
},
})
if (!user) {
return new Error(`No user found for email: ${email}`)
}
const passwordValid = await compare(password, user.PASSWORD)
if (!passwordValid) {
return new Error('Invalid password')
}
const token = await sign({ userId: user.id }, APP_SECRET)
return {
token ,
user,
}
},
})
},
})
My problem is when i try to mutate the login method with token return value , it works perfectly and here is my mutation
mutation{
login(email :"dondala422#hotmail.com" password :"aa")
{
token
}
}
Response
{
"data": {
"login": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjMyLCJpYXQiOjE1OTcxMDY0OTd9.d1Ra32ArCXumBfzg2vE1-xeea21cAkNwWBJPm3U3akM"
}
}
}
As shown . this works perfectly . now as mentioned the AuthPayload return the token and the users type
when i try to mutate with user :-
mutation{
login(email :"dondala422#hotmail.com" password :"aa")
{
token
users{
USERNAME
FIRST_NAME
}
}
}
it gives me this error
{
"errors": [
{
"message": "Cannot return null for non-nullable field AuthPayload.users.",
"locations": [
{
"line": 4,
"column": 5
}
],
"path": [
"login",
"users"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: Cannot return null for non-nullable field AuthPayload.users.",
" at completeValue (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:595:13)",
" at completeValueCatchingError (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:530:19)",
" at resolveField (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:461:10)",
" at executeFields (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:297:18)",
" at collectAndExecuteSubfields (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:748:10)",
" at completeObjectValue (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:738:10)",
" at completeValue (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:626:12)",
" at completeValue (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:592:21)",
" at C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:527:16"
]
}
}
}
],
"data": null
}
i tried to attach the debugger to see where is the null occur
and i didn't found any nullable values
here is a picture of vscode before return value , the token and user object are defined
VSCode Debugger picture

The object returned inside your resolver includes a property named user, but your field is named users. Since users is undefined, it resolves to null, but the field is non-nullable, so an error is thrown instead.

Related

Relationships with AwsCdk, DynamoDB and AppSync - Typescript and lambda functions

we are currently studying the stack: cdk, appsync and amplify to migrate our applications.
In our initial tests, we were able to upload a graphql api with only appsync wit relationships and it was very smooth, nice and fast.
When testing to build with cdk, we are having difficulties to create the relationships.
Here my code:
Schema
type Person {
id: ID!
name: String!
}
input PersonInput {
id: ID!
name: String!
}
input UpdatePersonInput {
id: ID!
name: String
}
type Client {
id: ID!
type: String!
personId: String
# Person: PersonConnection
Person: Person #connection(fields: ["personId"])
}
input ClientInput {
id: ID!
type: String!
personId: String!
}
input UpdateClientInput {
id: ID!
type: String
personId: String
}
My function
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
async function list() {
const params = {
TableName: process.env.CLIENT_TABLE,
}
try {
const data = await docClient.scan(params).promise()
return data.Items
} catch (err) {
console.log('DynamoDB error: ', err)
return null
}
}
export default list;
My table
const clientTable = new dynamodb.Table(scope, 'ClientTable', {
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
partitionKey: {
name: 'id',
type: dynamodb.AttributeType.STRING,
},
});
clientTable.addGlobalSecondaryIndex({
indexName: 'client-by-person-id',
partitionKey: {
name: 'personId',
type: dynamodb.AttributeType.STRING
},
sortKey: {
name: 'createdAt',
type: dynamodb.AttributeType.STRING
}
})
My query
query MyQuery {
listClients {
id
personId
type
Person {
name
}
}
}
However, my return to Person connection is null
"listClients": [
{
"id": "1",
"personId": "1",
"type": "PJ",
"Person": null
}
]
I would appreciate it if could point out our mistake
Solution of the problem based on the response of the Thorsten.
First, add resolver to the Person field in Client
export const clientResolvers = [{ typeName: "Client", fieldName: "Person" },...]
clientResolvers.map(((resolver: clientTypeResolver) => dataSource2.createResolver(resolver)))
Map function to the Person field in its lambda function
type AppSyncEvent = {
...
source: {personId: string,}
...
}
exports.handler = async (event:AppSyncEvent) => {
switch (event.info.fieldName) {
...
case "Person":
return await getPerson(event.source.personId);
}
}```
Function to solve the person field
async function getPerson(personId: string) {
console.log("CONTEXT\n" + JSON.stringify(personId, null, 2))
// console.log(context.source)
const params = {
TableName: process.env.PERSON_TABLE,
Key: { id: personId }
}
try {
const { Item } = await docClient.get(params).promise()
console.log("DATA\n" + JSON.stringify(Item, null, 2))
return Item
} catch (err) {
console.log('DynamoDB error: ', err)
}

graphql query with args not working for user id

I am bamboozled. I initially created the user query and it was giving me errors that I assumed were syntax errors. But then I created an identical query for vehicles which works perfectly. I have a suspicion that it's related to the ID! type but I have run out of leads. Any help would be appreciated!
Here are my typedefs and resolvers.
//TYPEDEFS//
type User {
id: ID!
fname: String
lname: String
email: String
password: String
vehicles: [Vehicle]
}
type Vehicle {
id: ID!
vin: String
model: String
make: String
drivers: [User]
}
type Query {
users: [User]
user(id: ID!): User
vehicles: [Vehicle]
vehicle(vin: String): Vehicle
}
//RESOLVERS//
user: async (parent, args, context) => {
const { id } = args
return context.prisma.user.findUnique({
where: {
id,
},
})
},
vehicle: async (parent, args, context) => {
const { vin } = args
return context.prisma.vehicle.findUnique({
where: {
vin,
}
})
}
//QUERY//
**This one is the broken one and has the error: Got invalid value '1' on prisma.findOneUser. Provided String, expected Int
**I've tried doing id: "1" and user(where: {id: 1})
query {
user(id:1){
id
fname
}
}
**This one works as intended
query {
vehicle(vin:"123123123"){
vin
make
}
}
//FULL ERROR*//
{
"errors": [
{
"message": "\nInvalid `prisma.user.findUnique()` invocation:\n\n{\n where: {\n id: '1'\n ~~~\n }\n}\n\nArgument id: Got invalid value '1' on prisma.findOneUser. Provided String, expected Int.\n\n",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"user"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"clientVersion": "2.13.1",
"stacktrace": [
"Error: ",
"Invalid `prisma.user.findUnique()` invocation:",
"",
"{",
" where: {",
" id: '1'",
" ~~~",
" }",
"}",
"",
"Argument id: Got invalid value '1' on prisma.findOneUser. Provided String, expected Int.",
"",
"",
" at Document.validate (/home/atran/workspace/m4m/m4m_server/node_modules/#prisma/client/runtime/index.js:76090:19)",
" at NewPrismaClient._executeRequest (/home/atran/workspace/m4m/m4m_server/node_modules/#prisma/client/runtime/index.js:77796:17)",
" at resource.runInAsyncScope (/home/atran/workspace/m4m/m4m_server/node_modules/#prisma/client/runtime/index.js:77733:52)",
" at AsyncResource.runInAsyncScope (async_hooks.js:188:21)",
" at NewPrismaClient._request (/home/atran/workspace/m4m/m4m_server/node_modules/#prisma/client/runtime/index.js:77733:25)",
" at Object.then (/home/atran/workspace/m4m/m4m_server/node_modules/#prisma/client/runtime/index.js:77850:39)",
" at process._tickCallback (internal/process/next_tick.js:68:7)"
]
}
}
}
],
"data": {
"user": null
}
}
Prisma expects an Int:
"Argument id: Got invalid value '1' on prisma.findOneUser. Provided
String, expected Int.",
Therefore you need to cast id to a number. Maybe something like this:
user: async (parent, args, context) => {
const id = +args.id;
return context.prisma.user.findUnique({
where: { id }
});
}

"Variable '$data' expected value of type 'VoteCreateInput"

When I am trying to do the "vote" mutation, getting the below error. My other mutations are working fine.
When I am trying to do the "vote" mutation, getting the below error. My other mutations are working fine.
When I am trying to do the "vote" mutation, getting the below error. My other mutations are working fine.
"data": null,
"errors": [
{
"message": "Variable '$data' expected value of type 'VoteCreateInput!' but
got: {\"user\":{\"connect\":
{\"id\":\"ck1j3nzi68oef090830r8wd6b\"}},\"link\":{\"connect\":
{\"id\":\"ck1j58loj8x570908njwe4eu7\"}}}. Reason: 'User' Expected non-null
value, found null. (line 1, column 11):\nmutation ($data:
VoteCreateInput!) {\n ^",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"vote"
]
}
]
Mutation
async function vote(parent, args, context, info) {
// 1
const userId = getUserId(context)
// 2
const linkExists = await context.prisma.$exists.vote({
user: { id: userId },
link: { id: args.linkId },
})
if (linkExists) {
throw new Error(`Already voted for link: ${args.linkId}`)
}
// 3
return context.prisma.createVote({
user: { connect: { id: userId } },
link: { connect: { id: args.linkId } },
})
}
datamodel.schema
type Link {
id: ID! #id
createdAt: DateTime! #createdAt
description: String!
url: String!
postedBy: User
votes: [Vote!]!
}
type User {
id: ID! #id
name: String!
email: String #unique
password: String!
links: [Link!]!
votes: [Vote!]!
}
type Vote {
id: ID! #id
link: Link!
user: User!
}
schema.graphql
type Mutation {
vote(linkId: ID!): Vote!
}
type Link {
id: ID!
description: String!
url: String!
postedBy: User
votes: [Vote!]!
}
There was a glitch in prisma database which was not updating with the change in data model.
I have created a new instance of the db and now its working fine.

Pass argument child type graphql

I would like to know how can I pass an argument in "child" type.
With the request, I would like to have only the message with id 1 from user id 1.
Currently I have this request :
{user(id:1){
email,
username,
messages(id:1){
text,
id
}
}}
I modified in the schema the User type, and changed
messages: [Message]
by
messages(id: Int): [Message]
But I have always all messages from user and not the message with specific id.
schema.js
import { gql } from 'apollo-server';
export const typeDefs = gql`
# declare custom scalars
scalar Date
##########
## TYPE ##
##########
# a group
type Group {
id: Int!
name: String
users: [User]!
messages: [Message]
}
# a user
type User {
id: Int! # unique id for the user
email: String!
username: String
messages(id: Int): [Message]
groups: [Group]
friends: [User]
}
# a message sent from a user to a group
type Message {
id: Int!
to: Group!
from: User!
text: String!
createdAt: Date!
}
###########
## QUERY ##
###########
# query for types
type Query {
user(email: String, id: Int): User
messages(groupId: Int, userId: Int): [Message]
}
schema {
query: Query
}
`;
export default typeDefs;
resolvers.js
import GraphQLDate from 'graphql-date';
import { Group, Message, User } from './connectors';
export const resolvers = {
Date: GraphQLDate,
Query: {
group(_, args) {
return Group.find({ where: args });
},
messages(_, args) {
return Message.findAll({
where: args,
order: [['createdAt', 'DESC']],
});
},
user(_, args) {
return User.findOne({ where: args });
},
},
Group: {
users(group) {
return group.getUsers();
},
messages(group) {
return Message.findAll({
where: { groupId: group.id },
order: [['createdAt', 'DESC']],
});
},
},
Message: {
to(message) {
return message.getGroup();
},
from(message) {
return message.getUser();
},
},
User: {
messages(user) {
return Message.findAll({
where: { userId: user.id },
order: [['createdAt', 'DESC']],
});
},
groups(user) {
return user.getGroups();
},
friends(user) {
return user.getFriends();
},
},
};
export default resolvers;
You have to update User.messages resolver. This way:
User: {
messages(user, { id }) {
return Message.findAll({
where: { userId: user.id, id },
order: [['createdAt', 'DESC']],
});
},
groups(user) {
return user.getGroups();
},
friends(user) {
return user.getFriends();
},
},
If you want to make id parameter optional, something like this may help:
messages(user, { id }) {
const where = { userId: user.id }
if (id) {
where.id = id
}
return Message.findAll({
where,
order: [['createdAt', 'DESC']],
});
},

GraphQL mutation without sub section

I want to send graphql mutation request without sub section
mutation _ {
updateCurrentUser(fullName: "Syava", email: "fake#gmail.com")
}
and I am getting
{
"errors": [
{
"message": "Field \"updateCurrentUser\" of type \"User\" must have a sub selection.",
...
}
]
}
add { id } to request works fine but I don't want
Also Schema code
const userType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLString) },
fullName: { type: GraphQLString },
email: { type: GraphQLString },
}),
});
type: userType,
args: {
fullName: { type: GraphQLString },
email: { type: new GraphQLNonNull(emailType) },
password: { type: GraphQLString },
},
resolve: async (root, { fullName, email, password }, { rootValue }) => {
const user = await User.findById(rootValue.req.user.id);
...
return user;
},
You define the type of the field to be UserType. Even though it's a mutation, it still follows the same rules and behavior as a query. Because UserType is an object type, it requires nested fields.
mutation _ {
updateCurrentUser(fullName: "Syava", email: "fake#gmail.com") {
fullName
email
}
}
// would respond with { fullName: 'Syava', email: 'fake#gmail.com' }
If you don't want the mutation to return a User, you can declare its type to GraphQLBoolean for example -- that's a scalar and doesn't have any nested fields.
{
type: GraphQLBoolean,
args: {
fullName: { type: GraphQLString },
email: { type: new GraphQLNonNull(emailType) },
password: { type: GraphQLString },
},
resolve: async (root, { fullName, email, password }, { rootValue }) => {
const user = await User.findById(rootValue.req.user.id);
user.fullName = fullName;
user.password = password; // or hashed to not store plain text passwords
return user.save(); // assuming save returns boolean; depends on the library you use
}
}
Note that the best practice for mutations in GraphQL APIs is to return a "result" object with multiple fields, such as the mutated object itself (e.g. user), clientMutationId (per Relay spec), and others as needed. This makes it flexible so you could add more data in the future.
updateCurrentUser(fullName: "Syava", email: "fake#gmail.com") {
clientMutationId
user {
...
}
}

Resources