|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { GraphQLScalarType, Kind } from "graphql"; |
| 21 | +import type { UniqueType } from "../../utils/graphql-types"; |
| 22 | +import { TestHelper } from "../../utils/tests-helper"; |
| 23 | + |
| 24 | +describe("https://github.com/neo4j/graphql/issues/6422", () => { |
| 25 | + const testHelper = new TestHelper(); |
| 26 | + let typeDefs: string; |
| 27 | + |
| 28 | + let MutationTest: UniqueType; |
| 29 | + const GraphQLUpperCaseString = new GraphQLScalarType({ |
| 30 | + name: "UpperCaseString", |
| 31 | + description: "The `UpperCaseString` scalar type returns all strings in upper case", |
| 32 | + serialize: (value) => { |
| 33 | + if (typeof value === "string") { |
| 34 | + return value.toUpperCase(); |
| 35 | + } |
| 36 | + |
| 37 | + throw new Error("Unknown type"); |
| 38 | + }, |
| 39 | + parseValue: (value) => { |
| 40 | + if (typeof value === "string") { |
| 41 | + return value.toUpperCase(); |
| 42 | + } |
| 43 | + |
| 44 | + throw new Error("Unknown type"); |
| 45 | + }, |
| 46 | + parseLiteral: (ast) => { |
| 47 | + if (ast.kind === Kind.STRING) { |
| 48 | + return ast.value.toUpperCase(); |
| 49 | + } |
| 50 | + |
| 51 | + return undefined; |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + beforeEach(async () => { |
| 56 | + MutationTest = testHelper.createUniqueType("MutationTest"); |
| 57 | + |
| 58 | + typeDefs = /* GraphQL */ ` |
| 59 | + scalar CustomScalar |
| 60 | + type ${MutationTest} @mutation(operations: [CREATE, UPDATE, DELETE]) @node @subscription(events: []) { |
| 61 | + enumValue: [EnumMutationTestEnumValue!]! |
| 62 | + myScalar: [CustomScalar!]! |
| 63 | + intValue: [Int!]! |
| 64 | + stringValue: [String!]! |
| 65 | + } |
| 66 | +
|
| 67 | + """ |
| 68 | + enum test |
| 69 | + """ |
| 70 | + enum EnumMutationTestEnumValue { |
| 71 | + ONE |
| 72 | + TWO |
| 73 | + THREE |
| 74 | + } |
| 75 | + `; |
| 76 | + |
| 77 | + await testHelper.executeCypher(` |
| 78 | + CREATE(:${MutationTest} { enumValue: ["ONE", "TWO"], myScalar: ["TEST", "TEST2"], intValue: [1, 2], stringValue: ["test", "test2"] }) |
| 79 | + `); |
| 80 | + |
| 81 | + await testHelper.initNeo4jGraphQL({ |
| 82 | + typeDefs, |
| 83 | + resolvers: { CustomScalar: GraphQLUpperCaseString }, |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + afterEach(async () => { |
| 88 | + await testHelper.close(); |
| 89 | + }); |
| 90 | + |
| 91 | + test("It should be possible to push an enum to an enum list", async () => { |
| 92 | + const query = /* GraphQL */ ` |
| 93 | + mutation { |
| 94 | + ${MutationTest.operations.update}( |
| 95 | + update: { |
| 96 | + enumValue: { |
| 97 | + push: [THREE] |
| 98 | + } |
| 99 | + } |
| 100 | + ) { |
| 101 | + ${MutationTest.plural} { |
| 102 | + enumValue |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + `; |
| 107 | + |
| 108 | + const queryResult = await testHelper.executeGraphQL(query); |
| 109 | + expect(queryResult.errors).toBeUndefined(); |
| 110 | + expect(queryResult.data).toEqual({ |
| 111 | + [MutationTest.operations.update]: { |
| 112 | + [MutationTest.plural]: [ |
| 113 | + { |
| 114 | + enumValue: expect.toIncludeSameMembers(["ONE", "TWO", "THREE"]), |
| 115 | + }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + test("It should be possible to push a custom scalar to an custom scalar list", async () => { |
| 122 | + const query = /* GraphQL */ ` |
| 123 | + mutation { |
| 124 | + ${MutationTest.operations.update}( |
| 125 | + update: { |
| 126 | + myScalar: { |
| 127 | + push: ["test3"] |
| 128 | + } |
| 129 | + } |
| 130 | + ) { |
| 131 | + ${MutationTest.plural} { |
| 132 | + myScalar |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + `; |
| 137 | + |
| 138 | + const queryResult = await testHelper.executeGraphQL(query); |
| 139 | + expect(queryResult.errors).toBeUndefined(); |
| 140 | + expect(queryResult.data).toEqual({ |
| 141 | + [MutationTest.operations.update]: { |
| 142 | + [MutationTest.plural]: [ |
| 143 | + { |
| 144 | + myScalar: expect.toIncludeSameMembers(["TEST", "TEST2", "TEST3"]), |
| 145 | + }, |
| 146 | + ], |
| 147 | + }, |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments