如何使用Apollo Playground在查询突变中输入自定义标量

时间:2019-06-29 20:05:37

标签: graphql

使用Apollo Playground测试使用包含自定义标量的输入Type对象的突变。字段lastWatched是Date的自定义标量类型。也许Date的解析器有问题,或者我只是想不出要在Playground中使用的正确语法。

我的自定义标量称为Date。这是我的Date解析器代码。

注意:由于功能parseLiteral(ast)中的问题,解析器代码已得到纠正。

    Date: new GraphQLScalarType({
    name: 'Date',
    description: 'Date custom scalar type',
    parseValue(value) {
      console.log('Date scalar parse:', value);
      return dayjs(value); // value from the client
    },
    serialize(value) {
      console.log('Date scalar serialize:', value);
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss'); // value sent to the client
    },
    parseLiteral(ast) {
      console.log('Date scalar parseLit:', ast);
      if (ast.kind === Kind.STRING) {
        return dayjs(ast.value).format('YYYY-MM-DD HH:mm:ss') || null; // ast value is always in string format
      }
      return null;
    }
  })

我的create突变突变的模式。

    extend type Mutation {
    createPlaybackPosition(fields: CreatePlaybackPositionInput): PlaybackPositionResponse
    updatePlaybackPosition(filename: ID!): PlaybackPositionResponse
    deletePlaybackPosition(filename: ID!): PlaybackPositionResponse
  }

  input CreatePlaybackPositionInput {
    "pseudo primary key"
    filename: ID!
    "in seconds"
    position: Int!
    "in seconds"
    duration: Int!
    "date and time (local)"
    lastWatched: Date!
  }

在Play Play Ground中,该突变看起来像:

 mutation {
  createPlaybackPosition(
    fields: {
      filename: "Test Show.S01E01.Piolet"
      position: 0
      duration: 0
      lastWatched: "06/27/2019 14:22:00"
    }
  ) {
    success
    code
    message
    validationErrors
  }
}

点击播放按钮时出现错误

  {
  "error": {
    "errors": [
      {
        "message": "Expected type Date!, found \"06/27/2019 14:22:00\"; value is not defined",
        "locations": [
          {
            "line": 2,
            "column": 111
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "exception": {
            "stacktrace": [
              "ReferenceError: value is not defined",
              "    at GraphQLScalarType.parseLiteral (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\src\\resolvers\\common.js:42:44)",
              "    at isValidScalar (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\node_modules\\graphql\\validation\\rules\\ValuesOfCorrectType.js:175:28)",
              "    at Object.StringValue (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\node_modules\\graphql\\validation\\rules\\ValuesOfCorrectType.js:144:14)",
              "    at Object.enter (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\node_modules\\graphql\\language\\visitor.js:332:29)",
              "    at Object.enter (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\node_modules\\graphql\\language\\visitor.js:383:25)",
              "    at visit (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\node_modules\\graphql\\language\\visitor.js:250:26)",
              "    at Object.validate (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\node_modules\\graphql\\validation\\validate.js:63:22)",
              "    at validate (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\node_modules\\apollo-server-core\\dist\\requestPipeline.js:202:32)",
              "    at Object.<anonymous> (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\node_modules\\apollo-server-core\\dist\\requestPipeline.js:123:42)",
              "    at Generator.next (<anonymous>)",
              "    at fulfilled (d:\\Users\\johni\\Projects\\graphql-knex-npvr\\node_modules\\apollo-server-core\\dist\\requestPipeline.js:4:58)",
              "    at processTicksAndRejections (internal/process/task_queues.js:89:5)"
            ]
          }
        }
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

如果三个标量方法(aHeadb)中的任何一个返回b,则GraphQL将抛出上述错误。返回Head基本上是一种告诉GraphQL接收到的值无效并且应该抛出验证错误的方法。您不会看到与a相同的行为,因为这是GraphQL中的有效值,但是如果返回def match_lists(a, b, acc \\ [] ) # Case: Element in both lists def match_lists( [%{"school" => school, "class" => class, "student" => student} | rest_a], [%{"school" => school, "class" => class, "choice" => choice} | rest_b], acc ) do element = build(school, class, student, [choice], true) match_lists(rest_a, rest_b, [element | acc]) end # Case: Element only in list B case. So it is a B case def match_lists( [%{"school" => school_a, "class" => class_a} | _] = a, [%{"school" => school_b, "class" => class_b, "choice" => choice} | rest_b], acc ) when school_a > school_b or class_a > class_b do element = build(school_b, class_b, nil, [choice], "only_list_b") match_lists(a, rest_b, [element | acc]) end # Case: No more elementes in A. So It is a B case def match_lists([], [%{"school" => school, "class" => class, "choice" => choice} | rest_b], acc) do element = build(school, class, nil, [choice], "only_list_b") match_lists([], rest_b, [element | acc]) end # Case: Element only in list A def match_lists( [%{"school" => school_a, "class" => class_a, "student" => student} | rest_a], [%{"school" => school_b, "class" => class_b} | _] = b, acc ) when school_b > school_a or class_b > class_a do element = build(school_a, class_a, student, [], "only_list_a") match_lists(rest_a, b, [element | acc]) end # Case: No more elementes in B. So It is an uncommon A case def match_lists([%{"school" => school, "class" => class, "student" => student} | rest_a], [], acc) do element = build(school, class, student, [], "only_list_a") match_lists(rest_a, [], [element | acc]) end def match_lists([], [], acc) do acc end defp build(school, class, student, choices, is_common) do %{ "school" => school, "class" => class, "student" => student, "choices" => choices, "is_common" => is_common } end ,也会出现上述错误。

在这种情况下,您要将iex(1)> match_lists(ordered_a, ordered_b) [ %{ "choices" => [], "class" => 6, "is_common" => "only_list_a", "school" => "c", "student" => "jane doe2" }, %{ "choices" => [], "class" => 9, "is_common" => "only_list_a", "school" => "b", "student" => "jane1 doe" }, %{ "choices" => ["maths"], "class" => 6, "is_common" => "only_list_b", "school" => "b", "student" => nil }, %{ "choices" => ["science"], "class" => 9, "is_common" => "only_list_b", "school" => "a", "student" => nil }, %{ "choices" => ["arts"], "class" => 1, "is_common" => true, "school" => "a", "student" => "jane doe" } ] 传递给serialize,但这不是您提供的字符串文字的值-它是ValueNode这样的对象:

parseLiteral

因此,您应该执行以下操作:

parseValue

如果您想始终返回null(如果值无效),并且永远不会抛出验证错误:

undefined