Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not generating code for subtypes only used as input to mutations? #2550

Closed
vrutberg opened this issue Oct 6, 2022 · 9 comments · Fixed by #2583
Closed

Not generating code for subtypes only used as input to mutations? #2550

vrutberg opened this issue Oct 6, 2022 · 9 comments · Fixed by #2583
Assignees
Labels
awaiting response blocking Prevents production or dev due to perf, bug, build error, etc.. bug Generally incorrect behavior codegen Issues related to or arising from code generation
Milestone

Comments

@vrutberg
Copy link

vrutberg commented Oct 6, 2022

Bug report

We have been attempting to move from apollo-ios 0.53.0 to 1.0.0 and have encountered a problem where code generation doesn't seem to generate code for subtypes that are only used as input. This worked for us using version 0.53.0.

Versions

Please fill in the versions you're currently using:

  • apollo-ios SDK version: 1.0.0
  • Xcode version: 14.0.1
  • Swift version: 5.7
  • Package manager: 5.7.0

Further details

Basically we have a mutation accepting a custom type as input. The type roughly looks like this:

enum CarProblem {
  NO_FUEL
}

input ReportCarProblemInput {
  carId: UUID!
  problem: CarProblem!
}

This is then used in a mutation:

mutation ReportCarProblem($input: ReportCarProblemInput!) {
  reportCarProblem(input: $input) {
    accepted
  }
}

When this code is generated, there's a compiler error where the generated code for ReportCarProblemInput references the CarProblem enum, because the code for it hasn't been generated.

If I manually add a queryable field of the CarProblem type, and a query for it, and then generate code, the generated code for ReportCarProblemInput works as expected.

@tngranados
Copy link

Same issue here. Generated code is not compiling due to it referencing objects that are not generated.

@calvincestari
Copy link
Member

Thanks for this bug report @vrutberg - we'll get a fix in ASAP and out in the coming 1.0.1 release.

@calvincestari calvincestari added bug Generally incorrect behavior codegen Issues related to or arising from code generation labels Oct 7, 2022
@calvincestari calvincestari added this to the Next Release (1.0.1) milestone Oct 11, 2022
@AnthonyMDev AnthonyMDev self-assigned this Oct 12, 2022
@AnthonyMDev
Copy link
Contributor

AnthonyMDev commented Oct 12, 2022

I'm very confused here. I cannot get this to reproduce right now. I've written this test, which is passing.

func testCompile_inputObject_referencingEnumType_referencedTypesContainsInputObjectAndEnum() throws {
    // given
    schemaSDL = """
    type Mutation {
      mutateCar(input: ReportCarProblemInput!): Car!
    }

    enum CarProblem {
      NO_FUEL
    }

    input ReportCarProblemInput {
      carId: UUID!
      problem: CarProblem!
    }

    scalar UUID

    interface Car {
      name: String!
    }
    """

    document = """
    mutation Test($input: ReportCarProblemInput!) {
      mutateCar(input: $input) {
        name
      }
    }
    """

    // when
    let compilationResult = try compileFrontend()

    let inputObject = compilationResult.referencedTypes
      .first { $0.name == "ReportCarProblemInput"} as? GraphQLInputObjectType

    let enumType = compilationResult.referencedTypes
      .first { $0.name == "CarProblem"} as? GraphQLEnumType

    // then
    expect(inputObject).toNot(beNil())
    expect(enumType).toNot(beNil())
  }

@vrutberg Does anything look different in the schema and mutation defined here from what you have in your graphql files?

Enough people are having this issue that I know it's a me problem right now haha.

@AnthonyMDev
Copy link
Contributor

I have run the entire code gen engine with the schema and operation above and I am getting a generated enum for CarProblem.

If someone who is experiencing this issue can create an example project that reproduces this to send to me, I would be really greatful!

@AnthonyMDev AnthonyMDev added blocking Prevents production or dev due to perf, bug, build error, etc.. awaiting response labels Oct 12, 2022
@tngranados
Copy link

I've tried your test @AnthonyMDev and it does indeed work, but if I create a GraphQL server with that schema, then introspect it and use the resulting JSON in the same test, it fails to find the enum type. Maybe the issue is in parsing the JSON schemas.

Here's the schema I'm using: https://gist.github.com/tngranados/8eee8b92dac430eacfdddf8f04fec721

I got from introspecting this node server:

var express = require('express');
const { graphqlHTTP } = require('express-graphql');
var { buildSchema } = require('graphql');
// GraphQL schema
var schema = buildSchema(`
  type Query {
    message: String
  }

  type Mutation {
    mutateCar(input: ReportCarProblemInput!): Car!
  }

  enum CarProblem {
    NO_FUEL
  }

  input ReportCarProblemInput {
    carId: UUID!
    problem: CarProblem!
  }

  scalar UUID

  interface Car {
    name: String!
  }
`);
// Root resolver
var root = {
    message: () => 'Hello World!'
};
// Create an express server and a GraphQL endpoint
var app = express();
app.use('/graphql', graphqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true
}));
app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));

@vrutberg
Copy link
Author

I think @tngranados is on to something, we are also using a JSON based schema derived from an introspection query.

@AnthonyMDev
Copy link
Contributor

Thanks for the info! I'll look into this now!

@AnthonyMDev
Copy link
Contributor

I was able to reproduce using JSON schema and have the fix now. Will be putting up a PR shortly! Thanks everyone!

@AnthonyMDev
Copy link
Contributor

Okay, the fix is now in main. We'll be releasing a 1.0.2 patch version including this fix in the coming days, but if you want to use it now, you can point to main!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response blocking Prevents production or dev due to perf, bug, build error, etc.. bug Generally incorrect behavior codegen Issues related to or arising from code generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants