Graphql query returning null for relational document, field missing from query but present in primsa admin

1.1k views Asked by At

When I view a document in prisma admin I see a location field but when I run queries against this database, the values are always null in the front end and are not being returned by the resolver at all.

I have looked at Why does a GraphQL query return null? but none of those cases match or speak to the root problem from what I can tell.

I've been trying to make sense of this article but haven't been able to implement such that I can compose a query that actually fires: https://www.prisma.io/tutorials/a-guide-to-common-resolver-patterns-ct08/

The setup matches Web Bos advanced react course and looks like:

Create React app -> Apollo-client -> Yoga server -> Prisma Server (sql)

I've gone back and forth using input types, simplifying the resolvers down to bare minimum, querying just a single document as opposed to an array of documents. The options I'd rather not pursue are using the Json scalar type and skipping the nested structure in favor of a flat structure.

Front end:

const CREATE_MISSION = gql`
  mutation CREATE_MISSION($title: String!, $description: String!, $bounty: Int!, $image: String, $postedBy: String!, $location: LocationInput) {
    createMission(title: $title, description: $description, bounty: $bounty, image: $image, postedBy: $postedBy, location: $location) {
      id
      title
      bounty
      image
      description
      postedBy
      location {
        id
        lat
        lng
      }
    }
  }
`;

schema.graphql:

input LocationInput {
  lat: Float!
  lng: Float!
}

type Mutation {
  createMission(
    title: String!,
    description: String!,
    bounty: Int!,
    image: String,
    postedBy: String!,
    location: LocationInput
  ): Mission!
}

datamodel.graphql:

type Location {
  id: ID! @id
  lat: Float!
  lng: Float!
}

type Mission {
  id: ID! @id
  title: String!
  description: String!
  bounty: Int!
  image: String
  postedBy: String!
  location: Location @relation(name: "MissionStart")
  end: Location @relation(name: "MissionEnd")
  dispatcher: User @relation(name: "Dispatcher")
  activeUser: User @relation(name: "Active")
  appliedUsers: [User] @relation(name: "Applicants")
}

Mutations.js

  async createMission (parent, args, ctx, info) {
    console.log(`Create Mission server side`)
    // let { input } = args
    console.dir(args)
    const mission = await ctx.db.mutation.createMission({
      data: {
        ...args,
        dispatcher: {
          connect: {
            id: args.postedBy
          },
        },
        location: {
          create: {
            lat: args.location.lat,
            lng: args.location.lng
          }
        }
      },
    }, info);
    return mission
  },

Playground query:

{
  mission (id:"someid") {
    title
    description
    location {
      lat
    }
  }
  location(id:"id_asPerTheDB ") {
    lat
  }
}

returns:

{
  "data": {
    "mission": {
      "title": "Take me the airport",
      "description": "so I can fly",
      "location": null
    },
    "location": {
      "lat": 30.2255867
    }
  }
}

As per the returned value in playground, I can't get that location out of the db with a simple nested query. I've tried using the following resolver (Query on backend) but this always has null for m.location

async missions (parent, args, ctx, info) {
    const missions = await ctx.db.query.missions()
    return Promise.all(missions.map(async m => {
      if (m.location) {
        const location = await ctx.db.query.location({where: { id: m.location.id }})
        return Object.assign(m, { location })
      } else return m
    }));
  },

and yet from prisma admin: prisma admin screenshot

1

There are 1 answers

0
alphanumeric0101 On

Wow, unreal, so the only thing I had to do was pass info to the Query...

async missions (parent, args, ctx, info) {
    const missions = await ctx.db.query.missions({}, info)
    return missions
  }

All the data I want is now passed to the client, info apparently has the details needed to handle the relation, good lord! 2 days later lol.