简体   繁体   中英

How can I pass an array of objects into Query?

I'm trying to figure out how to pass an array of objects into my GraphQL query, however i'm finding the documentation a little unclear on how to do so. I'm working with Apollo in the FE, Graphql-yoga in the BE and using Prisma as my database along with their API.

Here is my query with the array of objects hard coded:

const USERS = gql`

  query USERS(
    $userId: ID
  ) {
    users(
      where: {
        id_not: $userId
        hasProducts_some: {
          OR: [
            { itemId: 1 },
            { itemId: 2 }
          ]
        }
      }
    ) {
      firstName
    }
  }
`;

The above query returns me what I want, where i'm a bit stuck is how to get this array:

[
  { itemId: 1 },
  { itemId: 2 }
]

passed in as a variable of the query. From what I could find online, I might need to create a GraphQLObjectType on the client side to be able to pass in an object definition. Here was my implementation of that:

import { GraphQLObjectType, GraphQLString } from 'graphql';

const ProductName = new GraphQLObjectType({
  name: 'ProductName',
  fields: () => ({
    itemId: {
      type: GraphQLString,
    },
  })
});

const USERS = gql`

  query USERS(
    $userId: ID,
    $hasProducts: [ProductName]
  ) {
    users(
      where: {
        id_not: $userId
        hasProducts_some: {
          OR: $hasProducts
        }
      }
    ) {
      firstName
    }
  }
`;

The above returns me the following error:

Unknown type "ProductName"

Have I gone with the correct approach here for passing in arrays of objects, if so what's wrong with my implementation?

Types are created and used in creating your schema server-side. Once created, the schema cannot be modified at runtime -- it has whatever types and directives were specified when it was created. In other words, defining a new type on the client-side is meaningless -- it can't be used in any queries you send to the server since the server is not aware of the type.

If a variable (like $hasProducts ) is passed to an argument (like hasProducts_some ), that variable's type must match the type of the argument. This type could be a scalar (like String or Int ) or it could be an input object type. What exact type that that is depends on the schema itself. To determine the type to use, you can open up your schema's documentation in GraphQL Playground (or GraphiQL) and search for the field in question (in this case, hasProducts_some ).

Note that you can also just pass a single variable in for the whole where field.

Since the gql function expects a template literal, you should escape the product object like so:

const USERS = gql`

  query USERS(
    $userId: ID,
    $hasProducts: [${ProductName}]
  ) {
    users(
      where: {
        id_not: $userId
        hasProducts_some: {
          OR: $hasProducts
        }
      }
    ) {
      firstName
    }
  }
`;

New to graphql. But was wondering if this can resolve it.

const USERS = gql`
 query USERS(
    $userId: ID,
    $hasProducts: GraphQLList(ProductName) 
  ) {
    users(
      where: {
        id_not: $userId
        hasProducts_some: {
          OR: $hasProducts
        }
      }
    ) {
      firstName
    }
  }
`;

Minor change, but am not privileged to comment . So posting it as answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM