UNPKG

3.71 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * strict-local
8 * @format
9 */
10'use strict';
11
12/**
13 * Find the definition of a field of the specified type using strict
14 * resolution rules per the GraphQL spec.
15 */
16function getFieldDefinitionStrict(schema, parentType, fieldName, fieldAST) {
17 var type = require("./GraphQLSchemaUtils").getRawType(parentType);
18
19 var isQueryType = type === schema.getQueryType();
20
21 var hasTypeName = type instanceof require("graphql").GraphQLObjectType || type instanceof require("graphql").GraphQLInterfaceType || type instanceof require("graphql").GraphQLUnionType;
22
23 var schemaFieldDef;
24
25 if (isQueryType && fieldName === require("graphql").SchemaMetaFieldDef.name) {
26 schemaFieldDef = require("graphql").SchemaMetaFieldDef;
27 } else if (isQueryType && fieldName === require("graphql").TypeMetaFieldDef.name) {
28 schemaFieldDef = require("graphql").TypeMetaFieldDef;
29 } else if (hasTypeName && fieldName === require("graphql").TypeNameMetaFieldDef.name) {
30 schemaFieldDef = require("graphql").TypeNameMetaFieldDef;
31 } else if (type instanceof require("graphql").GraphQLInterfaceType || type instanceof require("graphql").GraphQLObjectType) {
32 schemaFieldDef = type.getFields()[fieldName];
33 }
34
35 return schemaFieldDef;
36}
37/**
38 * Find the definition of a field of the specified type, first trying
39 * the standard spec-compliant resolution process and falling back
40 * to legacy mode that supports fat interfaces.
41 */
42
43
44function getFieldDefinitionLegacy(schema, parentType, fieldName, fieldAST) {
45 var schemaFieldDef = getFieldDefinitionStrict(schema, parentType, fieldName, fieldAST);
46
47 if (!schemaFieldDef) {
48 var type = require("./GraphQLSchemaUtils").getRawType(parentType);
49
50 schemaFieldDef = getFieldDefinitionLegacyImpl(schema, type, fieldName, fieldAST);
51 }
52
53 return schemaFieldDef || null;
54}
55/**
56 * @private
57 */
58
59
60function getFieldDefinitionLegacyImpl(schema, type, fieldName, fieldAST) {
61 if (require("graphql").isAbstractType(type) && fieldAST && fieldAST.directives && fieldAST.directives.some(function (directive) {
62 return getName(directive) === 'fixme_fat_interface';
63 })) {
64 var possibleTypes = schema.getPossibleTypes(require("graphql").assertAbstractType(type));
65 var schemaFieldDef;
66
67 var _loop = function _loop(ii) {
68 var possibleField = possibleTypes[ii].getFields()[fieldName];
69
70 if (possibleField) {
71 // Fat interface fields can have differing arguments. Try to return
72 // a field with matching arguments, but still return a field if the
73 // arguments do not match.
74 schemaFieldDef = possibleField;
75
76 if (fieldAST && fieldAST.arguments) {
77 var argumentsAllExist = fieldAST.arguments.every(function (argument) {
78 return possibleField.args.find(function (argDef) {
79 return argDef.name === getName(argument);
80 });
81 });
82
83 if (argumentsAllExist) {
84 return "break";
85 }
86 }
87 }
88 };
89
90 for (var ii = 0; ii < possibleTypes.length; ii++) {
91 var _ret = _loop(ii);
92
93 if (_ret === "break") break;
94 }
95
96 return schemaFieldDef;
97 }
98}
99/**
100 * @private
101 */
102
103
104function getName(ast) {
105 var name = ast.name ? ast.name.value : null;
106
107 if (typeof name !== 'string') {
108 throw require("./RelayCompilerError").createCompilerError("Expected ast node to have a 'name'.", null, [ast]);
109 }
110
111 return name;
112}
113
114module.exports = {
115 getFieldDefinitionLegacy: getFieldDefinitionLegacy,
116 getFieldDefinitionStrict: getFieldDefinitionStrict
117};
\No newline at end of file