Skip to content

Commit c19ed6f

Browse files
committed
[language-plugin] Add documentation.
1 parent 97c58bf commit c19ed6f

File tree

1 file changed

+188
-17
lines changed

1 file changed

+188
-17
lines changed

Diff for: packages/relay-compiler/language/RelayLanguagePluginInterface.js

+188-17
Original file line numberDiff line numberDiff line change
@@ -16,60 +16,231 @@ const RelayConcreteNode = require('RelayConcreteNode');
1616

1717
import type {IRTransform, Root, Fragment} from 'graphql-compiler';
1818

19+
/**
20+
* A language plugin allows relay-compiler to both read and write files for any
21+
* language.
22+
*
23+
* When reading, the plugin is expected to parse and return GraphQL tags; and
24+
* when writing the plugin is responsible for generating type information about
25+
* the GraphQL selections made as well as generating the contents of the
26+
* artifact file.
27+
*
28+
* This interface describes the details relay-compiler requires to be able to
29+
* use the plugin. The plugin is expected to have as its main default export a
30+
* function that returns an object conforming to this interface.
31+
*/
32+
export type PluginInterface = {
33+
inputExtensions: string[],
34+
outputExtension: string,
35+
findGraphQLTags: GraphQLTagFinder,
36+
formatModule: FormatModule,
37+
typeGenerator: TypeGenerator,
38+
};
39+
1940
export type GraphQLTag = {
20-
keyName: ?string,
41+
/**
42+
* Should hold the string content of the `graphql` tagged template literal,
43+
* which is either an operation or fragment.
44+
*
45+
* @example
46+
*
47+
* grapqhl`query MyQuery { … }`
48+
* grapqhl`fragment MyFragment on MyType { … }`
49+
*/
2150
template: string,
51+
52+
/**
53+
* In the case this tag was part of a fragment container and it used a node
54+
* map as fragment spec, rather than a single tagged node, this should hold
55+
* the prop key to which the node is assigned.
56+
*
57+
* @example
58+
*
59+
* createFragmentContainer(
60+
* MyComponent,
61+
* {
62+
* keyName: graphql`fragment MyComponent_keyName { … }`
63+
* }
64+
* )
65+
*
66+
*/
67+
keyName: ?string,
68+
69+
/**
70+
* The location in the source file that the tag is placed at.
71+
*/
2272
sourceLocationOffset: {
23-
/* TODO: Is this also expected to yse 1-based index? */
73+
/**
74+
* The line in the source file that the tag is placed on.
75+
*
76+
* Lines use 1-based indexing.
77+
*/
2478
line: number,
25-
/* Should use 1-based index */
79+
80+
/**
81+
* The column in the source file that the tag starts on.
82+
*
83+
* Columns use 1-based indexing.
84+
*/
2685
column: number,
2786
},
2887
};
2988

89+
/**
90+
* This function is responsible for extracting `GraphQLTag` objects from source
91+
* files.
92+
*
93+
* @param {string} text The source file contents.
94+
* @param {string} filePath The path to the source file on disk.
95+
* @return {Array<GraphQLTag>} All extracted `GraphQLTag` objects.
96+
*
97+
* @see {@link javascript/FindGraphQLTags.js}
98+
*/
3099
export type GraphQLTagFinder = (
31100
text: string,
32101
filePath: string,
33102
) => Array<GraphQLTag>;
34103

35104
/**
36-
* Generate a module for the given document name/text.
105+
* The function that is responsible for generating the contents of the artifact
106+
* file.
107+
*
108+
* @see {@link javascript/formatGeneratedModule.js}
37109
*/
38110
export type FormatModule = ({|
111+
/**
112+
* The filename of the module.
113+
*/
39114
moduleName: string,
115+
116+
/**
117+
* The type of artifact that this module represents.
118+
*
119+
* @todo Document when this can be `empty`.
120+
*/
40121
documentType:
41122
| typeof RelayConcreteNode.FRAGMENT
42123
| typeof RelayConcreteNode.REQUEST
43124
| typeof RelayConcreteNode.BATCH_REQUEST
44125
| null,
126+
127+
/**
128+
* The actual document that this module represents.
129+
*/
45130
docText: ?string,
131+
132+
/**
133+
* The IR for the document that this module represents.
134+
*/
46135
concreteText: string,
136+
137+
/**
138+
* The type information generated for the GraphQL selections made.
139+
*/
47140
typeText: string,
48-
hash: ?string,
49-
devOnlyAssignments: ?string,
141+
142+
/**
143+
* The name of the relay-runtime module being used.
144+
*/
50145
relayRuntimeModule: string,
146+
147+
/**
148+
* A hash of the concrete node including the query text.
149+
*
150+
* @todo Document how this is different from `sourceHash`.
151+
*/
152+
hash: ?string,
153+
154+
/**
155+
* A hash of the document, which is used by relay-compiler to know if it needs
156+
* to write a new version of the artifact.
157+
*
158+
* @todo Is this correct? And document how this is different from `hash`.
159+
*/
51160
sourceHash: string,
161+
162+
/**
163+
* @todo Document this.
164+
*/
165+
devOnlyAssignments: ?string,
52166
|}) => string;
53167

168+
/**
169+
* The options that will be passed to the `generate` function of your plugin’s
170+
* type generator.
171+
*/
54172
export type TypeGeneratorOptions = {|
173+
/**
174+
* A map of custom scalars to scalars that the plugin knows about and emits
175+
* tpye information for.
176+
*
177+
* @example
178+
*
179+
* // The URL custom scalar is essentially a string and should be treated as
180+
* // such by the language’s type system.
181+
* { URL: 'String' }
182+
*/
55183
+customScalars: {[type: string]: string},
56-
+useHaste: boolean,
57-
+enumsHasteModule: ?string,
184+
185+
/**
186+
* Lists all other fragments relay-compiler knows about. Use this to know when
187+
* to import/reference other artifacts.
188+
*/
58189
+existingFragmentNames: Set<string>,
59-
+inputFieldWhiteList: $ReadOnlyArray<string>,
190+
191+
/**
192+
* The name of the relay-runtime module.
193+
*
194+
* This defaults to `relay-runtime`.
195+
*/
60196
+relayRuntimeModule: string,
197+
198+
/**
199+
* Whether or not relay-compiler will store artifacts next to the module that
200+
* they originate from or all together in a single directory.
201+
*
202+
* Storing all artifacts in a single directory makes it easy to import and
203+
* reference fragments defined in other artifacts without needing to use the
204+
* Haste module system.
205+
*
206+
* This defaults to `false`.
207+
*/
61208
+useSingleArtifactDirectory: boolean,
209+
210+
/**
211+
* @todo Document this.
212+
*/
213+
+inputFieldWhiteList: $ReadOnlyArray<string>,
214+
215+
/**
216+
* Whether or not the Haste module system is being used. This will currently
217+
* always be `false` for OSS users.
218+
*/
219+
+useHaste: boolean,
220+
221+
/**
222+
* @todo Document this.
223+
*/
224+
+enumsHasteModule: ?string,
62225
|};
63226

227+
/**
228+
* This object should hold the implementation required to generate types for the
229+
* GraphQL selections made.
230+
*
231+
* @see {@link javascript/RelayFlowGenerator.js}
232+
*/
64233
export type TypeGenerator = {
234+
/**
235+
* Transforms that should be applied to the intermediate representation of the
236+
* GraphQL document before passing to the `generate` function.
237+
*/
65238
transforms: Array<IRTransform>,
66-
generate: (node: Root | Fragment, options: TypeGeneratorOptions) => string,
67-
};
68239

69-
export type PluginInterface = {
70-
inputExtensions: string[],
71-
outputExtension: string,
72-
findGraphQLTags: GraphQLTagFinder,
73-
formatModule: FormatModule,
74-
typeGenerator: TypeGenerator,
240+
/**
241+
* Given GraphQL document IR, this function should generate type information
242+
* for e.g. the selections made. It can, however, also generate any other
243+
* content such as importing other files, including other artifacts.
244+
*/
245+
generate: (node: Root | Fragment, options: TypeGeneratorOptions) => string,
75246
};

0 commit comments

Comments
 (0)