Let's say I have a User type that contains the following fields:
type User {
name: String
username: String
someOtherField1: String
someOtherField2: String
someOtherField3: String
someOtherField4: String
creditCardNumber: String
}
If I am querying for myself, it's okay to return all fields, because the information is mine. So returning creditCardNumber
to the client is no biggie. But if I am querying for someone else, I should only be able to access the public info on the returned user. Returning creditCardNumber
would be terrible. And even if I don't code the query on the client to do so, what would prevent a malicious user from digging into the code, updating the client-side query to include creditCardNumber
, and executing it?
What is the best way to achieve this level of field restriction across queries in GraphQL? My only thought on this so far is to create a separate UserSearch
type, i.e.
type UserSearch {
name: String
username: String
someOtherField1: String
someOtherField2: String
someOtherField3: String
someOtherField4: String
}
which excludes the private fields, however this does not feel DRY as you'd be creating many types that are 90% similar in structure form each other.
Is there a cleaner way to implement this, that doesn't create unnecessary types or duplicate fields?