Why does the File interface not extend the Node interface in GraphQL?

Why does the File interface not extend the Node interface in GraphQL?

MarcoDiRich
Visitor
2 0 1

Dear Shopify Community,

 

whilest using the GraphiQL browser I stumbled upon an strange inconsistency and I hope somebody can help me understand why this is and if this is intended behavior:

 

The File interface does not extend the Node interface [see API ref.]

but querying node(id:__){...} I can use the polymorphism 'selector' with __typename ... on File{...}

 

this is not a very drastic issue but still a bit strage.

Hope someone knows why : )

 

Best regards,

Marco

Reply 1 (1)

oscprofessional
Shopify Partner
15961 2387 3095

Hello @MarcoDiRich 
Yes, you are correct. The File interface in GraphQL does not extend the Node interface. However, when using the `node(id: __)` query in GraphQL, you can still utilize the polymorphism feature by using the `__typename` field to differentiate between different types, including the File type.

The `__typename` field is a special field in GraphQL that allows you to retrieve the type name of a queried object. By using it in conjunction with the `... on TypeName` syntax, you can conditionally specify the fields you want to retrieve based on the type of the object.

Here's an example of how you can use the polymorphism and `__typename` with the File type:

```graphql
query {
node(id: "file_id") {
__typename
... on File {
id
filename
size
# Other File-specific fields you want to retrieve
}
}
}
```

In this example, you are querying the node with a specific `file_id`. The `__typename` field allows you to determine if the returned object is of type `File`. If it is, you can include the relevant fields specific to the File type, such as `id`, `filename`, and `size`.

By leveraging the `__typename` field and conditional fragments (`... on TypeName`), you can handle different types of objects in your GraphQL queries and retrieve the appropriate fields based on their types.

 

query {
  node(id: "file_id") {
    __typename
    ... on File {
      id
      filename
      size
      # Other File-specific fields you want to retrieve
    }
  }
}

Thank You

 

Get pass your Store Core Web Vital Free Speed Optimization Audit, Chat on WhatsApp | Skype : oscprofessionals-87 | Email: pallavi@oscprofessionals.com | Hire us | Guaranteed Site Speed Optimization | Website Free Audit | Shopify Theme Customization | Build Shopify Private App | Shopify SEO | Digital Marketing | Oscp Upsell & Cross sell App : Free | Oscp Sales & Volume Discount App : Free | Custom Pricing Wholesale App : Free