简体   繁体   中英

GraphQL-js Node/Express: How to pass a stringed object in a GraphQL Query?

My goal is to be able to pass a stringed object in a GraphQL query.

Goal:

 {
    accounts (filter: 
      '{"fieldName": "id",
      "fieldValues":["123"],
      "filterType":"in"}'){
        id
      }
 }

Error:

"Syntax Error GraphQL request (1:22) Unexpected single quote character ('), did you mean to use a double quote (\")?"

Problem is GraphQL doesn't seem to accept strings surrounded with single quotes.

Using single quotes inside of the object won't be possible because this would require major changes to the client which compiles these objects.

Example:

 { 
 "fieldName": "id",
 "fieldValues":["123"],
 "filterType":"in"
 }

Schema:

 accounts: {
   type: new GraphQLList(accountType),
   args: {
   id: { type: GraphQLString },
    status: { type: GraphQLString },
    filter: { type: GraphQLString },
    sort: { type: GraphQLString },
   },
   resolve: (root, args, { loaders }) => loaders.account.load(args),
},

You need escape double-qoutes inside query-string:

 {
  accounts (
   filter: "{\"fieldName\":\"id\",\"fieldValues\":[\"123\"],\"filterType\":\"in\"}"
  ){ id }
 }

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