relay icon indicating copy to clipboard operation
relay copied to clipboard

relay-compiler providing strange errors

Open johntran opened this issue 9 years ago • 11 comments

Ran

./node_modules/relay-compiler/bin/relay-compiler --schema ./server/graphql/schema.graphql --src ./client --out ./client

First error:

Writing default
Error: You supplied a GraphQL document with validation errors:
Fragment "AlcoholForm_recipientBirthDate" cannot condition on non composite type "Cart".
Field "recipientBirthDate" of type "String" must have a selection of subfields. Did you mean "recipientBirthDate { ... }"?

GraphQL Schema

type Cart {
  # The ID of an object
  id: ID!
  totalNumberOfItems: Int,
  recipientBirthDate: String

input CartUpdateRecipientBirthDateInput {
  recipientBirthDate: String!
  clientMutationId: String
}

AlcoholForm container

export default createFragmentContainer(
  AlcoholForm,
  graphql`
    fragment AlcoholForm_recipientBirthDate on Cart {
      recipientBirthDate 
    }
  `
)

I expected Cart to be a composite type because its an object. It seems odd that relay-compiler is asking for subfields when "recipientBirthDate" is a string.

Second error:

Writing default
Error: Can only create NonNull of a Nullable GraphQLType but got: CartUpdateRecipientBirthDateInput.

Mutation

export const CartUpdateRecipientBirthDateMutation = graphql`
  mutation CartUpdateRecipientBirthDateMutation(
    $input: CartUpdateRecipientBirthDateInput!
  ) {
    cartUpdateRecipientBirthDate(input: $input) {
        recipientBirthDate
    }
  }
`;

I ran the mutation in GraphiQL and it worked fine.

I'm not sure if these are bugs, or I fibbed my queries.

Let me know if you need more information.

johntran avatar Apr 16 '17 12:04 johntran

This is due to the change in isLeafType() if you downgrade the graphql-js package to 0.9.1 it should work or wait for a new alpha release of Relay with https://github.com/facebook/relay/pull/1611 merged

If you make the field nullable in the schema it should work with 0.9.2 though.

edvinerikson avatar Apr 16 '17 12:04 edvinerikson

Thanks for giving the compiler an early try and finding this case. Internally, we don't use non-nullable types a lot so we missed this case so far.

kassens avatar Apr 16 '17 17:04 kassens

  1. I downgraded to [email protected] on my app. Not sure if this will affect relay-compiler because the package.json for relay-compiler and relay is on [email protected]
  2. I integrated #1611 manually into the relay-compiler .bin file. This might not have the same result as building the .bin file because [email protected] isn't packaged with it. Still using [email protected]

GraphQL schema now looks like

input CartUpdateRecipientBirthDateInput {
  recipientBirthDate: String
  clientMutationId: String
}

type Cart {
  # The ID of an object
  id: ID!
  recipientBirthDate: String
}

Left the mutation untouched:

import { graphql } from 'react-relay/compat';

export const CartUpdateRecipientBirthDateMutation = graphql`
  mutation CartUpdateRecipientBirthDateMutation(
    $input: CartUpdateRecipientBirthDateInput!
  ) {
    cartUpdateRecipientBirthDate(input: $input) {
        recipientBirthDate
    }
  }
`;

Running the relay-compiler bin gets the same error:

Error: Can only create NonNull of a Nullable GraphQLType but got: CartUpdateRecipientBirthDateInput.

Package versions:

  1. [email protected]
  2. [email protected] w/ fix-leaf-type changes manually put in the bin file

Is there something I'm not making nullable?

The only thing I can think of is changing the mutation:

import { graphql } from 'react-relay/compat';

export const CartUpdateRecipientBirthDateMutation = graphql`
  mutation CartUpdateRecipientBirthDateMutation(
    $input: CartUpdateRecipientBirthDateInput
  ) {
    cartUpdateRecipientBirthDate(input: $input) {
        recipientBirthDate
    }
  }
`;

Which gets me:

Writing default
Error: You supplied a GraphQL document with validation errors:
Variable "$input" cannot be non-input type "CartUpdateRecipientBirthDateInput".
Field "recipientBirthDate" of type "String" must have a selection of subfields. Did you mean "recipientBirthDate { ... }"?

johntran avatar Apr 16 '17 21:04 johntran

Is it possible that two versions of graphql-js end up getting installed somehow? The compiler uses instanceof checks in many places, and if two versions are installed they can fail.

josephsavona avatar Apr 17 '17 00:04 josephsavona

Fix for the leaf type issue: https://github.com/facebook/relay/pull/1611

wincent avatar Apr 17 '17 00:04 wincent

I am still getting the same issue after building the compiler on master after @leebyron's fix.

It happens after trying to port one component to the modern API and running the compiler.

Fragment declaration:

export default createFragmentContainer(CoffeeShopCell, {
  coffeeShop: graphql`
    fragment CoffeeShopCell_coffeeShop on CoffeeShop {
      id
      name
    }
  `,
}),

Schema:

# An object with an ID
interface Node {
  # The id of the object.
  id: ID!
}

type CoffeeShop implements Node {
  # The ID of an object
  id: ID!
  name: String
}

Error:

Fragment "CoffeeShopCell_coffeeShop" cannot condition on non composite type "Cof
feeShop".
Field "id" of type "ID!" must have a selection of subfields. Did you mean "id {
... }"?
Field "name" of type "String" must have a selection of subfields. Did you mean "
name { ... }"?

janicduplessis avatar Apr 17 '17 22:04 janicduplessis

@josephsavona Looks like you are right about instanceof checks.

image

image

janicduplessis avatar Apr 17 '17 23:04 janicduplessis

I'm seeing this on the latest release

AndrewIngram avatar Apr 17 '17 23:04 AndrewIngram

#1617 should address the remaining issue

alangenfeld avatar Apr 18 '17 00:04 alangenfeld

Tested #1617 and it does work now!

janicduplessis avatar Apr 18 '17 00:04 janicduplessis

Can confirm alpha 4 fixes this :)

johntran avatar Apr 18 '17 03:04 johntran