Skip to content

Conversation

@hendrik-depauw-lemon
Copy link
Contributor

Description

The graphql type informer could not handle circular dependencies between types. The result would always be JSON. By using thunks, we can fix this issue.

Changes

  • Use thunks to define graphql fields

Checks

  • Project Builds
  • Project passes tests and checks
  • Updated documentation accordingly

Additional information

We are using Object.assign on the graphqlTypes map so that the references to the types do not change. However, when we want to update the fields of the type, we need an internal function from the graphql package that is currently not exported. What course of action do you guys suggest? Should we open a PR to graphql? Do we patch graphql? Do we copy the internals of the function? Other options?

@what-the-diff
Copy link

what-the-diff bot commented Mar 5, 2025

PR Summary

  • Utility Importation from graphql/type/definition
    The code now imports defineFieldMap function from the aforementioned module. This function will be used to help define fields in our program.

  • Introduction of Type Placeholder to Handle Circular References
    We have added a placeholder for types in both getOrCreateInputType and createOutputObjectType methods. This placeholder aids in managing and resolving circular references, thus ensuring that our code is less prone to errors and is more efficient.

  • Use of Object.assign for Object Creation
    The direct creation of GraphQLInputObjectType and GraphQLObjectType has been replaced. Instead, we've updated the this.graphQLTypes using Object.assign. By doing this,objects can be duplicated and merged more effectively, leading to better data management.

  • Field Definition through _fields Property
    In the input and output object types, we are now using the _fields property. Partnered with defineFieldMap utility, it helps in defining fields in our types.

  • Simplification of Return Statements
    The return statements were simplified to just return the existing type from this.graphQLTypes. This leads to cleaner code while maintaining efficiency.

Copy link
Collaborator

@MarcAstr0 MarcAstr0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change needed would be to work around the no longer accessible usage of defineFieldMap.

}, {}),
Object.assign(this.graphQLTypes[typeName], {
_fields: () =>
defineFieldMap({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hendrik-depauw-lemon, it appears that defineFieldMap is no longer accessible in graphql: ^16.6.0, the version used, causing the project not to build. It seems you can work around this by doing the following instead:

const inputObjectType = this.graphQLTypes[typeName] as GraphQLInputObjectType
Object.assign(inputObjectType, {
  _fields: () => {
    return finalFields.reduce((obj, prop) => {
      this.logger.debug(`Get or create GraphQL input type for property ${prop.name}`)
      return {
        ...obj,
        [prop.name]: { type: this.getOrCreateGraphQLType(prop.typeInfo, inputType) },
      }
    }, {})
  },
})


Object.assign(this.graphQLTypes[typeName], {
_fields: () =>
defineFieldMap({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hendrik-depauw-lemon, it's the same thing here regarding the usage of defineFieldMap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants