|
| 1 | +# RFC: Field Coordinates |
| 2 | + |
| 3 | +**Champion:** @magicmark |
| 4 | + |
| 5 | +This RFC proposes formalizing "field coordinates" - a way to uniquely identify a |
| 6 | +field defined in a GraphQL Schema. |
| 7 | + |
| 8 | +This may be listed as an appendix item in the official specification to serve as |
| 9 | +an official reference to third party library implementations. |
| 10 | + |
| 11 | +## 🚫 What this RFC does _not_ propose |
| 12 | + |
| 13 | +This RFC does not seek to change the GraphQL language in any way. |
| 14 | + |
| 15 | +- There are **no proposed GraphQL runtime changes** |
| 16 | +- There are **no proposed changes to how we parse documents**. |
| 17 | + |
| 18 | +## 📜 Problem Statement |
| 19 | + |
| 20 | +Third party GraphQL tooling and libraries may wish to refer to a field, or set of |
| 21 | +fields in a schema. Use cases include documentation, metrics and logging |
| 22 | +libraries. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +_(Example shown from GraphiQL's documentation search tab)_ |
| 27 | + |
| 28 | +There already exists a convention used by some third party libraries for writing |
| 29 | +out fields in a unique way for such purposes. However, there is no formal |
| 30 | +specification or name for this convention. |
| 31 | + |
| 32 | +## ✨ Worked Example |
| 33 | + |
| 34 | +For example, consider the following schema: |
| 35 | + |
| 36 | +```graphql |
| 37 | +type Person { |
| 38 | + name: String |
| 39 | +} |
| 40 | + |
| 41 | +type Business { |
| 42 | + name: String |
| 43 | + owner: Person |
| 44 | +} |
| 45 | + |
| 46 | +type Query { |
| 47 | + searchBusinesses(name: String): [Business] |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +We can write the following list of field coordinates: |
| 52 | + |
| 53 | +- `Person.name` uniquely identifies the "name" field on the "Person" type |
| 54 | +- `Business.name` uniquely identifies the "name" field on the "Business" |
| 55 | + type |
| 56 | +- `Business.owner` uniquely identifies the "owner" field on the "Business" type |
| 57 | +- `Query.searchBusinesses` uniquely identifies the "searchBusinesses" field on |
| 58 | + the "Query" type |
| 59 | + |
| 60 | +This RFC standardizes how we write field coodinates as above. |
| 61 | + |
| 62 | +## 🎨 Prior art |
| 63 | + |
| 64 | +- The name "field coordinates" comes from [GraphQL Java](https://github.com/graphql-java/graphql-java) |
| 65 | + (4.3k stars), where this is already used as described - [Github comment](https://github.com/graphql/graphql-spec/issues/735#issuecomment-646979049) - [Implementation](https://github.com/graphql-java/graphql-java/blob/2acb557474ca73/src/main/java/graphql/schema/FieldCoordinates.java) |
| 66 | + |
| 67 | +- GraphiQL displays field coordinates in its documentation search tab: |
| 68 | + |
| 69 | +  |
| 70 | + |
| 71 | +- [GraphQL Inspector](https://github.com/kamilkisiela/graphql-inspector) (840 stars) shows type/field pairs in its output: |
| 72 | + |
| 73 | +  |
| 74 | + |
| 75 | +- [Apollo Studio](https://www.apollographql.com/docs/studio/) shows field |
| 76 | + coodinates when hovering over fields in a query: |
| 77 | + |
| 78 | +  |
| 79 | + |
| 80 | +## 🗳️ Alternatives considered |
| 81 | + |
| 82 | +### Naming |
| 83 | + |
| 84 | +- "type/field pairs" was the original suggestion |
| 85 | + |
| 86 | + However, "field coordinates" was chosen as it is already understood and used by |
| 87 | + the popular [GraphQL Java](https://github.com/graphql-java/graphql-java) |
| 88 | + project. |
| 89 | + |
| 90 | +- "Field path" / "GraphQL path" |
| 91 | + |
| 92 | + [`path` exists as an attribute on `GraphQLResolveInfo`](https://github.com/graphql/graphql-js/blob/8f3d09b54260565/src/type/definition.js#L951). |
| 93 | + |
| 94 | + Given the following query: |
| 95 | + |
| 96 | + ```graphql |
| 97 | + query { |
| 98 | + searchBusinesses(name: "El Greco Deli") { |
| 99 | + name |
| 100 | + owner { |
| 101 | + name |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + ``` |
| 106 | + |
| 107 | + The field coordinate `Person.name` may also be written as the following field |
| 108 | + path: |
| 109 | + |
| 110 | + ```json |
| 111 | + ["query", "searchBusinesses", 1, "owner", "name"] |
| 112 | + ``` |
| 113 | + |
| 114 | + Note that here, the "path" is a serialized _response_ tree traversal, instead |
| 115 | + of describing the location of the field in the _schema_. |
| 116 | + |
| 117 | + Since "path" is already used in GraphQL nonclemanture to describe the location |
| 118 | + of a field in a response, we'll avoid overloading this term. |
| 119 | + |
| 120 | +### Separator |
| 121 | + |
| 122 | +This RFC proposes using "`.`" as the separator character. The following have |
| 123 | +also been proposed: |
| 124 | + |
| 125 | +- `Foo::bar` |
| 126 | +- `Foo#bar` |
| 127 | +- `Foo->bar` |
| 128 | +- `Foo~bar` |
| 129 | +- `Foo:bar` |
| 130 | + |
| 131 | +"`.`" is already used in the existing implementations of field coordinates, hence |
| 132 | +the suggested usage in this RFC. However, we may wish to consider one of the |
| 133 | +alternatives above, should this conflict with existing or planned language |
| 134 | +features. |
| 135 | + |
| 136 | +## 🤔 Drawbacks / Open questions |
| 137 | + |
| 138 | +- https://github.com/graphql/graphql-spec/issues/735 discusses potential |
| 139 | + conflicts with the upcoming namespaces proposal - would like to seek clarity on |
| 140 | + this |
| 141 | + |
| 142 | +- **Is this extensible enough?** The above issue discusses adding arguments as |
| 143 | + part of this specifcation - we haven't touched on this here in order to keep |
| 144 | + this RFC small, but we may wish to consider this in the future (e.g. |
| 145 | + `Query.searchBusiness:name`). |
| 146 | + |
| 147 | +- **How will this play with namespaces?** Not sure what this looks like yet! |
| 148 | + |
| 149 | +- **Would we want to add a method to graphql-js?** A `fieldCoordinateToFieldNode` |
| 150 | + method (for example) may take in a field coordinate string and return a field |
| 151 | + AST node to serve as a helper / reference implementation of the algorithm to |
| 152 | + look up the field node. |
0 commit comments