Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

After transform custom scalars are broken #9

Closed
Nikamura opened this issue Jul 2, 2019 · 2 comments
Closed

After transform custom scalars are broken #9

Nikamura opened this issue Jul 2, 2019 · 2 comments

Comments

@Nikamura
Copy link

Nikamura commented Jul 2, 2019

import { createTestClient } from "apollo-server-testing";
import { GraphQLScalarType, Kind } from "graphql";
import { makeExecutableSchema } from "graphql-tools";
import { transformSchema } from "graphql-tools-fork";

import { GraphQLServer } from "@bootstrapping/apollo-server/graphql-server";

export const myCustomScalarType = new GraphQLScalarType({
  name: "MyCustomScalar",
  description: "Description of my custom scalar type",
  parseValue: (_value: string) => "parseValue: value",
  serialize: (value: string) => `serialize: ${value} ` + JSON.stringify(value),
  parseLiteral: ast => {
    if (ast.kind === Kind.STRING) {
      return ast.value;
    }
    return null;
  }
});

const schemaString = `

scalar MyCustomScalar

type Foo {
  aField: MyCustomScalar
}

type Query {
  foo: Foo
  myFoo2: MyCustomScalar
}

`;

const resolverFunctions = {
  MyCustomScalar: myCustomScalarType,
  Query: {
    foo: {
      aField: () => "myFoo"
    },
    myFoo2: () => {
      return "James";
    }
  }
};

const schema = makeExecutableSchema({
  typeDefs: schemaString,
  resolvers: resolverFunctions
});

const client = createTestClient(new GraphQLServer({ schema }));

const query = "query myFoo { foo { aField } myFoo2 }";
client.query({ query }).then(results => {
  console.log("client", results.data!.myFoo2);
});

const transformedSchema = transformSchema(schema, []);
const transformedClient = createTestClient(new GraphQLServer({ schema: transformedSchema }));

transformedClient.query({ query }).then(results => {
  console.log("transformedClient", results.data!.myFoo2);
});

After running this the output is:

client serialize: James "James"
transformedClient serialize: parseValue: value "parseValue: value"

so the value passed to serialize is different than before schema was transformed.

@yaacovCR
Copy link
Owner

yaacovCR commented Jul 2, 2019

This may be working as intended, transformSchema actually wraps the original schema and delegates to it, so we have to serialize with original schema and then parse with new. in general, shouldn't the serialize and parse functions reverse each other?

@yaacovCR
Copy link
Owner

yaacovCR commented Jul 3, 2019

I think this is working as designed:

consider changing code as below to gain visibility:

export const myCustomScalarType = new GraphQLScalarType({
  name: "MyCustomScalar",
  description: "Description of my custom scalar type",
  //parseValue: (_value: string) => "parseValue: value",
  parseValue: (value: string) => `parseValue: ${value}`,
  serialize: (value: string) => `serialize: ${value} ` + JSON.stringify(value),
  parseLiteral: ast => {
    if (ast.kind === Kind.STRING) {
      return ast.value;
    }
    return null;
  }
});

@yaacovCR yaacovCR closed this as completed Jul 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants