zth / rescript-relay

Use Relay with ReScript.
https://rescript-relay-documentation.vercel.app/docs/getting-started
339 stars 53 forks source link

`makeConnectionId` function type #392

Closed mununki closed 2 years ago

mununki commented 2 years ago

I face a type error in generated makeConnectionId function with latest 1.0.0-rc.5

let makeConnectionId = (
  connectionParentDataId: RescriptRelay.dataId,
  ~sort: [
    | #PRICE_ASC
    | #PRICE_DESC
    | #PRICE_PER_KG_ASC
    | #PRICE_PER_KG_DESC
    | #UPDATED_ASC
    | #UPDATED_DESC
  ],
  ~onlyBuyable: bool=Js.null, // Error
  (),
) => ...

I guess, onlyBuyable argument can't be a bool type with a default value Js.null. I've read the changelog, but let me know if I miss something.

zth commented 2 years ago

Good catch! I'll fix this and put out a new test package you can try. Let me get back to you once I have done that.

zth commented 2 years ago

@mattdamon108 can you try 0.0.0-connection-id-maker-fix-389056d3?

mununki commented 2 years ago

Can you explain how to install it? It doesn't seem the branch name.

zth commented 2 years ago

Just setting the version in package.json to that should work, ie "rescript-relay": "0.0.0-connection-id-maker-fix-389056d3"

mununki commented 2 years ago

It seems fixed! But I have another issue with the connection. My schema is like

module Fragment = %relay(`
  fragment UpdateProductOptionsAdminFragment on Product {
    id
    displayName
    ...ProductSummaryAdminFragment

    ... on NormalProduct {
      isCourierAvailable
      productOptions(first: 20)
        @connection(key: "UpdateProductOptionsAdmin_productOptions") {
        __id
        edges {
          node {
            id
            status
            ...UpdateProductOptionAdminFragment
          }
        }
      }
    }

    ... on QuotableProduct {
      isCourierAvailable
      productOptions(first: 20)
        @connection(key: "UpdateProductOptionsAdmin_productOptions") {
        __id
        edges {
          node {
            id
            status
            ...UpdateProductOptionAdminFragment
          }
        }
      }
    }
  }
`)

The value getConnectionNodes can't be found in UpdateProductOptionsAdminFragment_graphql is thrown. I've read the doc https://rescript-relay-documentation.vercel.app/docs/mutations#updating-connections, but makeConnectionId function signature is different from the doc.

image
zth commented 2 years ago

Got it, I know what the problem is. Let me think a bit on how to solve it best.

zth commented 2 years ago

@mattdamon108 could you paste the contents of that generated file? UpdateProductOptionsAdminFragment_graphql.

mununki commented 2 years ago

Sorry, I'm late 😄

/* @sourceLoc Update_ProductOptions_Admin.res */
/* @generated */
%%raw("/* @generated */")
module Types = {
  @@ocaml.warning("-30")

  type rec fragment_productOptions_edges_node = {
    @live id: string,
    status: RelaySchemaAssets_graphql.enum_ProductOptionStatus,
    fragmentRefs: RescriptRelay.fragmentRefs<[ | #UpdateProductOptionAdminFragment]>,
  }
  and fragment_productOptions_edges = {
    node: fragment_productOptions_edges_node,
  }
  and fragment_productOptions = {
    @live __id: RescriptRelay.dataId,
    edges: array<fragment_productOptions_edges>,
  }
  type fragment = {
    @live __typename: string,
    displayName: string,
    @live id: string,
    isCourierAvailable: option<bool>,
    productOptions: option<fragment_productOptions>,
    fragmentRefs: RescriptRelay.fragmentRefs<[ | #ProductSummaryAdminFragment]>,
  }
}

module Internal = {
  @live
  type fragmentRaw
  @live
  let fragmentConverter: Js.Dict.t<Js.Dict.t<Js.Dict.t<string>>> = %raw(
    json`{"__root":{"productOptions_edges_node":{"f":""},"":{"f":""}}}`
  )
  @live
  let fragmentConverterMap = ()
  @live
  let convertFragment = v => v->RescriptRelay.convertObj(
    fragmentConverter,
    fragmentConverterMap,
    Js.undefined
  )
}

type t
type fragmentRef
external getFragmentRef:
  RescriptRelay.fragmentRefs<[> | #UpdateProductOptionsAdminFragment]> => fragmentRef = "%identity"

@live
@inline
let connectionKey = "UpdateProductOptionsAdmin_productOptions"

%%private(
  @live @module("relay-runtime") @scope("ConnectionHandler")
  external internal_makeConnectionId: (RescriptRelay.dataId, @as("UpdateProductOptionsAdmin_productOptions") _, 'arguments) => RescriptRelay.dataId = "getConnectionID"
)

let makeConnectionId = (connectionParentDataId: RescriptRelay.dataId, ) => {
  let args = ()
  internal_makeConnectionId(connectionParentDataId, args)
}

module Utils = {
  @@ocaml.warning("-33")
  open Types
  @live
  external productOptionStatus_toString: RelaySchemaAssets_graphql.enum_ProductOptionStatus => string = "%identity"
  @live
  external productOptionStatus_input_toString: RelaySchemaAssets_graphql.enum_ProductOptionStatus_input => string = "%identity"
  @live
  let productOptionStatus_decode = (enum: RelaySchemaAssets_graphql.enum_ProductOptionStatus): option<RelaySchemaAssets_graphql.enum_ProductOptionStatus_input> => {
    switch enum {
      | #...RelaySchemaAssets_graphql.enum_ProductOptionStatus_input as valid => Some(valid)
      | _ => None
    }
  }
  @live
  let productOptionStatus_fromString = (str: string): option<RelaySchemaAssets_graphql.enum_ProductOptionStatus_input> => {
    productOptionStatus_decode(Obj.magic(str))
  }
}

type relayOperationNode
type operationType = RescriptRelay.fragmentNode<relayOperationNode>

let node: operationType = %raw(json` (function(){
var v0 = {
  "count": null,
  "cursor": null,
  "direction": "forward",
  "path": [
    "productOptions"
  ]
},
v1 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "__typename",
  "storageKey": null
},
v2 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "id",
  "storageKey": null
},
v3 = [
  {
    "alias": null,
    "args": null,
    "kind": "ScalarField",
    "name": "isCourierAvailable",
    "storageKey": null
  },
  {
    "alias": "productOptions",
    "args": null,
    "concreteType": "ProductOptionConnection",
    "kind": "LinkedField",
    "name": "__UpdateProductOptionsAdmin_productOptions_connection",
    "plural": false,
    "selections": [
      {
        "alias": null,
        "args": null,
        "concreteType": "ProductOptionEdge",
        "kind": "LinkedField",
        "name": "edges",
        "plural": true,
        "selections": [
          {
            "alias": null,
            "args": null,
            "concreteType": "ProductOption",
            "kind": "LinkedField",
            "name": "node",
            "plural": false,
            "selections": [
              (v2/*: any*/),
              {
                "alias": null,
                "args": null,
                "kind": "ScalarField",
                "name": "status",
                "storageKey": null
              },
              {
                "args": null,
                "kind": "FragmentSpread",
                "name": "UpdateProductOptionAdminFragment"
              },
              (v1/*: any*/)
            ],
            "storageKey": null
          },
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "cursor",
            "storageKey": null
          }
        ],
        "storageKey": null
      },
      {
        "alias": null,
        "args": null,
        "concreteType": "PageInfo",
        "kind": "LinkedField",
        "name": "pageInfo",
        "plural": false,
        "selections": [
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "endCursor",
            "storageKey": null
          },
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "hasNextPage",
            "storageKey": null
          }
        ],
        "storageKey": null
      },
      {
        "kind": "ClientExtension",
        "selections": [
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "__id",
            "storageKey": null
          }
        ]
      }
    ],
    "storageKey": null
  }
];
return {
  "argumentDefinitions": [],
  "kind": "Fragment",
  "metadata": {
    "connection": [
      (v0/*: any*/),
      (v0/*: any*/)
    ]
  },
  "name": "UpdateProductOptionsAdminFragment",
  "selections": [
    (v1/*: any*/),
    (v2/*: any*/),
    {
      "alias": null,
      "args": null,
      "kind": "ScalarField",
      "name": "displayName",
      "storageKey": null
    },
    {
      "args": null,
      "kind": "FragmentSpread",
      "name": "ProductSummaryAdminFragment"
    },
    {
      "kind": "InlineFragment",
      "selections": (v3/*: any*/),
      "type": "NormalProduct",
      "abstractKey": null
    },
    {
      "kind": "InlineFragment",
      "selections": (v3/*: any*/),
      "type": "QuotableProduct",
      "abstractKey": null
    }
  ],
  "type": "Product",
  "abstractKey": "__isProduct"
};
})() `)
zth commented 2 years ago

I'm having trouble reproducing this. 2 things:

  1. Would you please try cleaning node_modules, rerun the Relay compiler etc, and see if it still happens?
  2. If it still happens, would you be up for adding a test to this repo that reproduces the problem? I'll guide you.
mununki commented 2 years ago

I think you can reproduce with https://github.com/green-labs/sinsunhi-frontend-mirror/tree/rescript-relay-test

mununki commented 2 years ago

And I'll try to make and add a test to reproduce too!

mununki commented 2 years ago

I followed the LOCAL_DEVELOPMENT.md. But I'm afraid I can go further. Can you guide the steps how to build and test. I'm trying to run a test in packages/rescript-relay. Before that shouldn't I build the whole packages maybe?

zth commented 2 years ago

Will have a look!

zth commented 2 years ago

@mattdamon108 can you try 0.0.0-connection-id-maker-fix-24be613b? Pretty sure that should fix it.

mununki commented 2 years ago

@zth Perfect! It compiles fine. Thank you for your prompt fix!