question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No way to have optional data in GraphQL queries.

See original GitHub issue

Description

I’m not sure this is a bug or a feature-request. It seems that while a specific <page-query> can have nullable GraphQL fields, if you want to query a source that you’ve personally defined it does not allow nullable entries.

Steps to reproduce

Using the gridsome/gridsome-starter-blog try and create a post without a cover image. The only way to do so is by rewriting the Page component (and Index etc) to not rely on the cover_image.

Expected result

It should be possible to mark fields as optional or nullable, or just natively support it.

Actual result

Module build failed (from ./node_modules/gridsome/lib/plugins/vue-components/lib/loaders/page-query.js):
Error: Cannot query field "cover_image" on type "Post".

GraphQL request (124:5)
123:     content
124:     cover_image (width: 860, blur: 10)
         ^
125:     meta_image (width: 860, blur: 10)

Environment


Libs:
- gridsome version: 0.6.2
- @gridsome/cli version: 0.1.1

Browser:
- [X] Chrome (desktop) version 74.0.3729.169
 
For Tooling issues:
- Node version: 12.0
- Platform:  Mac

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
jltxwesleycommented, Dec 10, 2019

@Acidic9 you can create a Post that contains all the fields, just give it some random name and exclude it from showing up in your site. Better yet is if you have a Post that is actually used that contains all the fields, you just need 1.

Closing this though since it will be fixed in v0.7

Not sure if it’s fixed in v0.7? I am still experiencing the same issue.

7reactions
hjvedvikcommented, May 31, 2019

Gridsome can only generate the schema based on the data you have. We are working on APIs for letting you defined the schema yourself to make sure the cover_image always exists. It will be available in v0.7 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using nullability in GraphQL
By default, every field in GraphQL is nullable, and you can opt in to mark it non-null. In this article, we'll go over...
Read more >
GraphQL optional Query Arguments - Stack Overflow
Arguments in GraphQL are nullable (i.e. optional) by default. So if your type definition looks like this: companies(limit: Int): [Company].
Read more >
Querying with GraphQL optional arguments in Elm - Thoughtbot
We update the GraphQL schema to have a nullable notes field. The notes argument is optional. We can tell because it doesn't have...
Read more >
Nulls in GraphQL: Cheatsheet - Hasura
A GraphQL query can have fields and inputs, with or without variables. Fields are always optional, whether nullable or not.
Read more >
Queries and Mutations - GraphQL
Most discussions of GraphQL focus on data fetching, but any complete data platform needs a way to modify server-side data as well. In...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found