Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/graphql/language/SourceLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@


import graphql.PublicApi;
import graphql.schema.GraphQLModifiedType;
import graphql.schema.GraphQLNamedSchemaElement;
import graphql.schema.GraphQLSchemaElement;
import graphql.schema.GraphQLTypeUtil;
import graphql.schema.idl.SchemaGenerator;

import java.io.Serializable;
import java.util.Objects;
Expand Down Expand Up @@ -74,4 +79,27 @@ public String toString() {
(sourceName != null ? ", sourceName=" + sourceName : "") +
'}';
}


/**
* This method can return {@link SourceLocation} that help create the given schema element. If the
* schema is created from input files and {@link SchemaGenerator.Options#isCaptureAstDefinitions()}
* is set to true then schema elements contain a reference to the {@link SourceLocation} that helped
* create that runtime schema element.
*
* @param schemaElement the schema element
*
* @return the source location if available or null if it's not.
*/
public static SourceLocation getLocation(GraphQLSchemaElement schemaElement) {
if (schemaElement instanceof GraphQLModifiedType) {
schemaElement = GraphQLTypeUtil.unwrapAllAs((GraphQLModifiedType) schemaElement);
}
if (schemaElement instanceof GraphQLNamedSchemaElement) {
Node<?> node = ((GraphQLNamedSchemaElement) schemaElement).getDefinition();
return node != null ? node.getSourceLocation() : null;
}
return null;
}

}
71 changes: 71 additions & 0 deletions src/test/groovy/graphql/language/SourceLocationTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package graphql.language

import graphql.parser.MultiSourceReader
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.SchemaGenerator
import graphql.schema.idl.SchemaParser
import spock.lang.Specification

import static graphql.schema.FieldCoordinates.coordinates

class SourceLocationTest extends Specification {

def "can get source location"() {
def sdl = """
type Query {
a : A!
}

type A {
b : B
}

type B {
c : String
}
"""

def sourceReader = MultiSourceReader.newMultiSourceReader().string(sdl, "sourceName").build()

def definitionRegistry = new SchemaParser().parse(sourceReader)
when:
def schema = new SchemaGenerator().makeExecutableSchema(definitionRegistry, RuntimeWiring.MOCKED_WIRING)
def schemaElement = schema.getType("Query")
def location = SourceLocation.getLocation(schemaElement)

then:
location.sourceName == "sourceName"
location.line == 2
location.column == 13

when:
schemaElement = schema.getFieldDefinition(coordinates("Query", "a"))
location = SourceLocation.getLocation(schemaElement)

then:
location.sourceName == "sourceName"
location.line == 3
location.column == 17

when:
schemaElement = schema.getFieldDefinition(coordinates("Query", "a")).getType()
// unwrapped
location = SourceLocation.getLocation(schemaElement)

then:
location.sourceName == "sourceName"
location.line == 6
location.column == 13

when:
schemaElement = schema.getType("A")
location = SourceLocation.getLocation(schemaElement)

then:
location.sourceName == "sourceName"
location.line == 6
location.column == 13


}
}