graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Encounter graphql-compose-relay error when upgrade to 9.0

Open blueandhack opened this issue 5 years ago • 4 comments

When I upgrade to version 9.0 The relay plugin throw error

throw new Error(`ObjectTypeComposer(${tc.getTypeName()}) should have recordIdFn. ` + 'This function returns ID from provided object.');

This is my code

import { composeMongoose } from 'graphql-compose-mongoose'
import { composeWithRelay } from 'graphql-compose-relay'
const customizationOptions = {}

export const CreateBaseComposer = QueryModel => {
  const ModelTC = composeWithRelay(
    composeMongoose(QueryModel, customizationOptions)
  )
  return ModelTC
}

blueandhack avatar Oct 01 '20 10:10 blueandhack

Are you using relay classic or modern?

For Relay modern, there is no need to use graphql-compose-relay.

nodkz avatar Oct 01 '20 10:10 nodkz

Are you using relay classic or modern?

For Relay modern, there is no need to use graphql-compose-relay.

Alright, I think I use Relay modern, I just create a simple example project to try, then it works. Thanks. BTW, for my current production project, when I upgrade to version 9, I face a lot of function names that need changes, it is too hard to upgrade the old project. But I figure out the new version has a lot of new features I desired, hahaha. 😂

blueandhack avatar Oct 03 '20 09:10 blueandhack

@nodkz Let me start by saying this is an amazing library! Thank you!

I have a problem with integration of graphql-compose-relay into v9. My client is running relay experimental and I use the @refetchable directive.

@refetchable directive can only be added to fragments that are "refetchable", that is, on fragments that are on Viewer, or on Query, or on a type that implements Node (i.e. a type that has an id field).

I tried providing a recordIdFn to my mongoose type composer but since the v9 api does not support .getResolver for findById anymore hence building the schema fails. Here's the reference to the code that causes the failure. Are there any workarounds?

farshid1 avatar Feb 15 '21 08:02 farshid1

Ok so I looked at the source code a little bit more and noticed that a few other APIs are also exposed from the graphql-compose-relay library. I basically manually did what the library does for all of my TCs:

import composeWithRelay, {
  getNodeInterface,
  toGlobalId,
} from 'graphql-compose-relay';
import {schemaComposer} from 'graphql-compose';

composeWithRelay(schemaComposer.Query);

[TC1, TC2, ...].forEach((tc) => {
  tc.addInterface(getNodeInterface(schemaComposer));
  tc.addFields({
    id: {
      type: 'ID!',
      description: 'The globally unique ID among all types',
      resolve: (source) => toGlobalId(tc.getTypeName(), tc.getRecordId(source)),
    },
  });
  tc.setRecordIdFn((source) => source._id);
});

So far I was able to build the project both on the server and client sides without any problems. I will let you know if this solution does not work for some reason!

Again thanks for all of these libraries. We've been having a blast with them!

farshid1 avatar Feb 15 '21 09:02 farshid1