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

Extend custom Types and Interfaces #20969

Closed
Jhony0311 opened this issue Jan 28, 2020 · 8 comments
Closed

Extend custom Types and Interfaces #20969

Jhony0311 opened this issue Jan 28, 2020 · 8 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. topic: GraphQL Related to Gatsby's GraphQL layer type: documentation An issue or pull request for improving or updating Gatsby's documentation

Comments

@Jhony0311
Copy link
Contributor

Jhony0311 commented Jan 28, 2020

Summary

Hey Gatsby people,

I'm starting a project where my data source is a set of JSON files. In order to make them a bit more robust, I would like to get them extender using custom types. I already put something in place for custom types nested inside of a field of my JSON but the things are not working for me when I tried to do an interface for an array.

I was looking into the documentation for schema customization (https://www.gatsbyjs.org/docs/schema-customization/) but the portion for interfaces is not as detailed as I would like in order to complete the setup myself.

I'm thinking that I might be missing to create one resolver for the field that exposes the interface but not sure what is the best way to go for it.

My expectation is to get something like this as an output of the Graphql API:

{
    "__typename": "TriVideo",
    "kind": "TriVideo",
}

But what I'm getting right now is:

{
  "errors": [
    {
      "message": "Cannot read property 'type' of undefined",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "pagesJson",
        "contentBlocks",
        0
      ],
      "stack": [
        "TypeError: Cannot read property 'type' of undefined",
        "    at typeComposer.setResolveType.node (/sandbox/node_modules/gatsby/dist/schema/schema.js:471:57)",
        "    at completeAbstractValue (/sandbox/node_modules/graphql/execution/execute.js:652:21)",
        "    at completeValue (/sandbox/node_modules/graphql/execution/execute.js:585:12)",
        "    at completeValueCatchingError (/sandbox/node_modules/graphql/execution/execute.js:495:19)",
        "    at /sandbox/node_modules/graphql/execution/execute.js:618:25",
        "    at Array.forEach (<anonymous>)",
        "    at forEach (/sandbox/node_modules/iterall/index.js:83:25)",
        "    at completeListValue (/sandbox/node_modules/graphql/execution/execute.js:614:24)",
        "    at completeValue (/sandbox/node_modules/graphql/execution/execute.js:573:12)",
        "    at completeValueCatchingError (/sandbox/node_modules/graphql/execution/execute.js:495:19)",
        "    at resolveField (/sandbox/node_modules/graphql/execution/execute.js:435:10)",
        "    at executeFields (/sandbox/node_modules/graphql/execution/execute.js:275:18)",
        "    at collectAndExecuteSubfields (/sandbox/node_modules/graphql/execution/execute.js:713:10)",
        "    at completeObjectValue (/sandbox/node_modules/graphql/execution/execute.js:703:10)",
        "    at completeValue (/sandbox/node_modules/graphql/execution/execute.js:591:12)",
        "    at /sandbox/node_modules/graphql/execution/execute.js:492:16"
      ]
    },
    {
      "message": "Cannot read property 'type' of undefined",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "pagesJson",
        "contentBlocks",
        1
      ],
      "stack": [
        "TypeError: Cannot read property 'type' of undefined",
        "    at typeComposer.setResolveType.node (/sandbox/node_modules/gatsby/dist/schema/schema.js:471:57)",
        "    at completeAbstractValue (/sandbox/node_modules/graphql/execution/execute.js:652:21)",
        "    at completeValue (/sandbox/node_modules/graphql/execution/execute.js:585:12)",
        "    at completeValueCatchingError (/sandbox/node_modules/graphql/execution/execute.js:495:19)",
        "    at /sandbox/node_modules/graphql/execution/execute.js:618:25",
        "    at Array.forEach (<anonymous>)",
        "    at forEach (/sandbox/node_modules/iterall/index.js:83:25)",
        "    at completeListValue (/sandbox/node_modules/graphql/execution/execute.js:614:24)",
        "    at completeValue (/sandbox/node_modules/graphql/execution/execute.js:573:12)",
        "    at completeValueCatchingError (/sandbox/node_modules/graphql/execution/execute.js:495:19)",
        "    at resolveField (/sandbox/node_modules/graphql/execution/execute.js:435:10)",
        "    at executeFields (/sandbox/node_modules/graphql/execution/execute.js:275:18)",
        "    at collectAndExecuteSubfields (/sandbox/node_modules/graphql/execution/execute.js:713:10)",
        "    at completeObjectValue (/sandbox/node_modules/graphql/execution/execute.js:703:10)",
        "    at completeValue (/sandbox/node_modules/graphql/execution/execute.js:591:12)",
        "    at /sandbox/node_modules/graphql/execution/execute.js:492:16"
      ]
    },
    {
      "message": "Cannot read property 'type' of undefined",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "pagesJson",
        "contentBlocks",
        2
      ],
      "stack": [
        "TypeError: Cannot read property 'type' of undefined",
        "    at typeComposer.setResolveType.node (/sandbox/node_modules/gatsby/dist/schema/schema.js:471:57)",
        "    at completeAbstractValue (/sandbox/node_modules/graphql/execution/execute.js:652:21)",
        "    at completeValue (/sandbox/node_modules/graphql/execution/execute.js:585:12)",
        "    at completeValueCatchingError (/sandbox/node_modules/graphql/execution/execute.js:495:19)",
        "    at /sandbox/node_modules/graphql/execution/execute.js:618:25",
        "    at Array.forEach (<anonymous>)",
        "    at forEach (/sandbox/node_modules/iterall/index.js:83:25)",
        "    at completeListValue (/sandbox/node_modules/graphql/execution/execute.js:614:24)",
        "    at completeValue (/sandbox/node_modules/graphql/execution/execute.js:573:12)",
        "    at completeValueCatchingError (/sandbox/node_modules/graphql/execution/execute.js:495:19)",
        "    at resolveField (/sandbox/node_modules/graphql/execution/execute.js:435:10)",
        "    at executeFields (/sandbox/node_modules/graphql/execution/execute.js:275:18)",
        "    at collectAndExecuteSubfields (/sandbox/node_modules/graphql/execution/execute.js:713:10)",
        "    at completeObjectValue (/sandbox/node_modules/graphql/execution/execute.js:703:10)",
        "    at completeValue (/sandbox/node_modules/graphql/execution/execute.js:591:12)",
        "    at /sandbox/node_modules/graphql/execution/execute.js:492:16"
      ]
    }
  ],
  "data": {
    "pagesJson": {
      "contentBlocks": [
        null,
        null,
        null
      ]
    }
  }
}

Environment

I've created a minimal setup of my local within CodeSandbox
https://codesandbox.io/s/quirky-pare-bk01t

@devrchancay
Copy link
Contributor

I think this can help you: https://learnwithjason.dev/advanced-graphql-techniques-in-gatsby

At first glance everything looks good, I am also in the mobile when I get home I will see the detailed mind on my computer

@pieh pieh added the topic: GraphQL Related to Gatsby's GraphQL layer label Jan 29, 2020
@pieh
Copy link
Contributor

pieh commented Jan 29, 2020

Can you share some minimal reproduction? It's hard to say what's going on without seeing source data and what you schema customization looks like

@Jhony0311
Copy link
Contributor Author

I did, is part of the issue. Here you can see what I have: https://codesandbox.io/s/quirky-pare-bk01t

@vladar
Copy link
Contributor

vladar commented Jan 29, 2020

@Jhony0311 Thanks for opening this! We lack documentation on how to create custom interfaces indeed.

The missing piece in your sandbox is defining resolveType method for the Block interface. This method accepts an incoming value and decides the specific object type of this value (using some discriminator, in your case kind).

So what you could do is to define this interface separately:

exports.createSchemaCustomization = ({ actions, schema }) => {
  const { createTypes } = actions

  const blockInterface = schema.buildInterfaceType({
    name: `Block`,
    fields: {
      kind: `String!`,
    },
    resolveType: value => value.kind,
  })

  const typeDefs = `
        type PagesJson implements Node {
            image: File @fileByRelativePath
            example1: TriVideo
            example2: ThoughtBlock
            example3: TwoColumnWrapped
            contentBlocks: [Block]
        }

        type TriVideo implements Block {
            kind: String!
            video1: String!
            video2: String!
            video3: String!
        }

        type ThoughtBlock implements Block {
            kind: String!
            orientation: String!
            text: String!
        }

        type TwoColumnWrapped implements Block {
            kind: String!
            imageSide: String!
            text: String!
            images: [File!] @fileByRelativePath
        }
    `

  createTypes([blockInterface, typeDefs])
}

I've forked your codesandbox, so you can see a working version here: https://codesandbox.io/s/practical-monad-57x37 (also I don't think you need createResolvers at all - you only need those when your graphql fields are different from source value fields)

I hope it helps.

@vladar vladar added the type: documentation An issue or pull request for improving or updating Gatsby's documentation label Jan 29, 2020
@Jhony0311
Copy link
Contributor Author

That worked perfectly @vladar I appreciate it. I also appreciate the explanation, helped me a lot to understand what's happening and what I need to do to accomplish what I'm looking for.

Is there any improvements to the docs that I could help with? I'm not a graphql expert myself but I'll be more than happy to jump on anything that could help people to get things clearer.

@marcysutton
Copy link
Contributor

@Jhony0311 we have Using Schema Customization coming up as a workflow to evaluate next quarter. If there are things you feel could be improved in the docs in the meantime, we would welcome a PR! Another way to contribute would be to get a jump start on the workflow evaluation as outlined here (# 15): #13708

@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Feb 19, 2020
@github-actions
Copy link

Hey again!

It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it.
Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community! 💪💜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. topic: GraphQL Related to Gatsby's GraphQL layer type: documentation An issue or pull request for improving or updating Gatsby's documentation
Projects
None yet
Development

No branches or pull requests

5 participants