Giter Site home page Giter Site logo

Comments (5)

yaacovCR avatar yaacovCR commented on September 13, 2024

This seems fixable as we currently perform reparsing of enums based on the simple info.returnType of a field without checking for unions. Lists should work, though. Can you check?

See https://github.com/yaacovCR/graphql-tools-fork/blob/master/src/stitching/checkResultAndHandleErrors.ts#L70

You could maybe move the pace of a fix along just by including more of your test case code? Or submitting a PR for handling unions similar to how lists are handled.

I do not think this should be any different whether you are using fragments or not. Can you check that, too?

Thanks for submitting!

from graphql-tools-fork.

AndKiel avatar AndKiel commented on September 13, 2024

Here's a snippet with a minimalistic reproduction:

import { ApolloServer } from "apollo-server-express";
import { createTestClient } from "apollo-server-testing";
import { GraphQLSchema } from "graphql";
import { GraphQLResponse } from "graphql-extensions";
import { FilterRootFields, transformSchema } from "graphql-tools-fork";
import { buildSchemaSync, createUnionType, Field, ObjectType, Query, registerEnumType, Resolver } from "type-graphql";

function createTestSchema(): GraphQLSchema {
  enum TestEnum {
    One = "bug",
    Two = "issue"
  }

  registerEnumType(TestEnum, {
    name: "TestEnum"
  });

  @ObjectType()
  class TestObject {
    @Field(_type => TestEnum)
    public type: TestEnum = TestEnum.One;
  }

  @ObjectType()
  class SecondTestObject {
    @Field(_type => TestEnum)
    public type: TestEnum = TestEnum.Two;

    @Field(_type => [TestEnum])
    public typeArray: TestEnum[] = [TestEnum.One, TestEnum.Two]
  }

  const testUnionType = createUnionType({
    name: "TestUnionType",
    types: [TestObject, SecondTestObject]
  });

  @Resolver(_of => TestObject)
  class TestResolver {
    @Query(_type => testUnionType)
    public getUnion(): TestObject | SecondTestObject {
      return new SecondTestObject();
    }
  }

  return buildSchemaSync({
    resolvers: [TestResolver]
  });
}

function execute(schema: GraphQLSchema, query: string): Promise<GraphQLResponse> {
  const server = new ApolloServer({ schema });
  return createTestClient(server).query({ query });
}

const schema = createTestSchema();
const transformedSchema = transformSchema(schema, [
  new FilterRootFields((_operation, _fieldName, _field) => true)
]);

it("preserves enum on fragment when schema is transformed", async () => {
  const query = "query { getUnion { ... on SecondTestObject { type } } }";
  const response = await execute(schema, query);
  const transformedResponse = await execute(transformedSchema, query);

  expect(response).toEqual(transformedResponse);
  expect(transformedResponse).toEqual({
    data: {
      getUnion: {
        type: "Two"
      }
    }
  })
});

it("preserves enum array on fragment when schema is transformed", async () => {
  const query = "query { getUnion { ... on SecondTestObject { typeArray } } }";
  const response = await execute(schema, query);
  const transformedResponse = await execute(transformedSchema, query);

  expect(response).toEqual(transformedResponse);
  expect(transformedResponse).toEqual({
    data: {
      getUnion: {
        type: ["One", "Two"]
      }
    }
  })
});

Covers both enum and enum array for union type.
It works fine when it's not a union.

from graphql-tools-fork.

yaacovCR avatar yaacovCR commented on September 13, 2024

Looks like we should add another case here:

https://github.com/yaacovCR/graphql-tools-fork/blob/master/src/stitching/checkResultAndHandleErrors.ts#L73

Pseudocode: (isUnionType(type) && type.resolveType(value) === (GraphqlEnumType || GraphqlScalarType))

Do you want to give that a try in PR?

from graphql-tools-fork.

yaacovCR avatar yaacovCR commented on September 13, 2024

I actually misunderstood your bug, I thought it was of unions of enums, not unions of object types with enum fields. (And it turns out you cannot even have union of scalar types, only that of object types!)

The problem in this case was therefore not in the parseOutputValue logic, but rather that defaultMergedResolver was not getting called for unions (and probably interfaces!)

That should now be fixed. Here's hoping.

from graphql-tools-fork.

AndKiel avatar AndKiel commented on September 13, 2024

Sorry for the confusion, I thought that the snippet made it clear.
It's fixed now, thank you!

from graphql-tools-fork.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.