Skip to content

When updating data, an error occurs if there is a relationship #73

Open
@nomin-sasabuchi

Description

@nomin-sasabuchi

Prerequisites

Operation has been confirmed for data acquisition, creation, and updating when there is no relationship

Problem

If you request an update for a schema that has the following relationships, the following error will occur. Why?
If I exclude the related property (status: ScheduleState!), it works, so I think it's a problem with the relation.
Do I need to write my own resolver in such a case?

Error: Cannot return null for non-nullable field Schedule.status.: {"response":{"errors":[{"message":"Cannot return null for non-nullable field Schedule.status.","locations":[{"line":11,"column":5}],"path":["updateSchedule","status"]}],"data":{"updateSchedule":null},

Code

//schema
type Mutation {
  updateSchedule(id: ID!,params: UpdateScheduleInput): Schedule
}

type Schedule {
  id: ID!
  startAt: DateTime
  endAt: DateTime
  status: ScheduleState!
}

type ScheduleState {
  id: ID!
  name: String!
}


input UpdateScheduleInput {
  id: ID!
  startAt: DateTime
  endAt: DateTime
  statusId: ID
}

//query
export const updateSchedule = gql`
  mutation updateSchedule($id: ID!, $params: UpdateScheduleInput) {
    updateSchedule(id: $id, params: $params) {
      id
      startAt
      endAt
      status {
        id
        name
      }
    }
  }
`

Tried

By the way, I tried the following resolver, but it didn't work

import {
  createGraphQLHandler,
  mirageGraphQLFieldResolver,
} from '@miragejs/graphql'
import { createServer } from 'miragejs'

import schema from './generated-mock-schema'

export default createServer({
  seeds(server) {
    const status0 = server.create('ScheduleState', {
      id: '0',
      name: 'making',
    })
    const status1 = server.create('ScheduleState', {
      id: '1',
      name: 'confirm',
    })
    server.create('Schedule', {
      id: '1',
      startAt: '2023-12-01 11:05:09',
      endAt: '2023-12-01 13:05:09',
      status: status0,
    })
    server.create('Schedule', {
      id: '2',
      startAt: '2023-12-01 13:05:09',
      endAt: '2023-12-01 15:05:09',
      status: status1,
    })
  },
  routes() {
    const resolvers = {
      Mutation: {
        updateSchedule(obj, args, context, info) {
          const { params } = args

          const schedules = context.mirageSchema.db.schedules.find(args.id)
          const statusData = context.mirageSchema.db.scheduleStates.find(
            schedules.statusId
          )
          const records = {
            ...schedules,
           status: statusData,
            ...params,
          }
          return context.mirageSchema.db.schedules.update(args.id, records)
        },
      },
    }
    this.post(
      import.meta.env.VITE_SCHEDULE_API_URL,
      // @ts-ignore
      createGraphQLHandler(schema, this.schema, { resolvers })
    )
  },
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions