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.

How to add local only fields for interface type Objects?

See original GitHub issue

I’m working on a Chat Application which have several types of Messages. Our graphql schema is defined as below. (we call a chat as ‘counsel’ in this project)

interface Message {
  id: ID!
  author: User!
  counsel: Counsel!
  createdAt: DateTime!
}
type TextMessage implements Message {
  id: ID!
  author: User!
  counsel: Counsel!
  createdAt: DateTime!
  body: String!
}
type FileMessage implements Message {
  id: ID!
  author: User!
  counsel: Counsel!
  createdAt: DateTime!
  file: File!
}
type ShopMessage implements Message {
  id: ID!
  author: User!
  counsel: Counsel!
  createdAt: DateTime!
  shop: Shop!
}
type InvoiceMessage implements Message {
  id: ID!
  author: User!
  counsel: Counsel!
  createdAt: DateTime!
  order: Order!
}
# etc...

For the frontend react application, I wanted to add local only field status on Message interface to control UI of message sending status.

Expected Behavior

// src/utils/cache.ts
// use local only 'status' field on every type of Messages!
export const typeDefs = gql`
  enum MessageStatus {
    PENDING
    SENT
    FAILED
  }
  extend interface Message {
    status: MessageStatus!
  }
`;

When I run graphql codegenerator after writing code as above, it gave me Error log as below.

    Error: Type EndWarningMessage must only implement Interface types, it
 cannot implement Message.

    Type EnterMessage must only implement Interface types, it cannot impl
ement Message.

    Type FileMessage must only implement Interface types, it cannot imple
ment Message.

    Type InvoiceMessage must only implement Interface types, it cannot im
plement Message.

    Type LeaveMessage must only implement Interface types, it cannot impl
ement Message.

    Type RestartMessage must only implement Interface types, it cannot im
plement Message.

    Type ShopMessage must only implement Interface types, it cannot imple
ment Message.

    Type TextMessage must only implement Interface types, it cannot imple
ment Message.

What I had to do to work

After reading Error log and trying several times, I’ve succeed to let Application work, but I had to add status field for every implemented Message types as described below.

// src/utils/cache.ts
export const typeDefs = gql`
  enum MessageStatus {
    PENDING
    SENT
    FAILED
  }
  extend type TextMessage {
    status: MessageStatus!
  }
  extend type FileMessage {
    status: MessageStatus!
  }
  extend type ShopMessage {
    status: MessageStatus!
  }
  extend type InvoiceMessage {
    status: MessageStatus!
  }
`;

Question

Is there any cool way to extend my Message interface, which can add my status field to every type of Messages at once?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
benjamncommented, Oct 20, 2021

@Hemistone I believe GCG is able to generate typed typePolicies via StrictTypedTypePolicies, though I haven’t tried it myself, so I’d be interested to hear how it goes!

0reactions
jpvajdacommented, Jun 1, 2022

I’m going to close this one out as this issue had a response to help the reported problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Local-only fields in Apollo Client - Apollo GraphQL Docs
Fetch local and remote data with one GraphQL query. Your Apollo Client queries can include local-only fields that aren't defined in your GraphQL...
Read more >
Handbook - Interfaces - TypeScript
How to write an interface with TypeScript. ... Some properties should only be modifiable when an object is first created. You can specify...
Read more >
Understanding and using interfaces in TypeScript
A class has three different types of variables: Local variables: a local variable is defined at the function or block level. It exists...
Read more >
Create the new service interface - IBM
In the Name field, enter FullName and click Finish. The business object editor opens. To create a new field, click the little F...
Read more >
Interface Tutorial - Appian 20.3
Click New Application. In the Name field, type Appian Tutorial. Optionally, in the Description field, add a short description. Click Create.
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