How to add local only fields for interface type Objects?
See original GitHub issueI’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:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Hemistone I believe GCG is able to generate typed
typePolicies
viaStrictTypedTypePolicies
, though I haven’t tried it myself, so I’d be interested to hear how it goes!I’m going to close this one out as this issue had a response to help the reported problem.