|
| 1 | +package graphql.language |
| 2 | + |
| 3 | +import graphql.GraphQLError |
| 4 | +import graphql.InvalidSyntaxError |
| 5 | +import graphql.TestUtil |
| 6 | +import graphql.execution.preparsed.PreparsedDocumentEntry |
| 7 | +import graphql.validation.ValidationError |
| 8 | +import graphql.validation.ValidationErrorType |
| 9 | +import spock.lang.Specification |
| 10 | + |
| 11 | +class SerialisationTest extends Specification { |
| 12 | + |
| 13 | + static <T> T serialisedDownAndBack(T inputObj) { |
| 14 | + def file = File.createTempFile("serialised", ".bin") |
| 15 | + |
| 16 | + FileOutputStream f = new FileOutputStream(file) |
| 17 | + ObjectOutputStream o = new ObjectOutputStream(f) |
| 18 | + |
| 19 | + o.writeObject(inputObj) |
| 20 | + o.close() |
| 21 | + |
| 22 | + FileInputStream fi = new FileInputStream(file) |
| 23 | + ObjectInputStream oi = new ObjectInputStream(fi) |
| 24 | + |
| 25 | + T object = oi.readObject() as T |
| 26 | + oi.close() |
| 27 | + |
| 28 | + return object |
| 29 | + } |
| 30 | + |
| 31 | + static SourceLocation srcLoc(int line, int col) { |
| 32 | + new SourceLocation(line, col) |
| 33 | + } |
| 34 | + |
| 35 | + def query = ''' |
| 36 | + #comment |
| 37 | + query HeroForEpisode($ep: Episode!) { |
| 38 | + hero(episode: $ep) { |
| 39 | + name |
| 40 | + ... on Droid { |
| 41 | + primaryFunction |
| 42 | + } |
| 43 | + ... on Human { |
| 44 | + height |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + rightComparison: hero(episode: JEDI) { |
| 49 | + ...comparisonFields |
| 50 | + } |
| 51 | + |
| 52 | + differentArgObjects( s : "s", int : 1, b : true, list : [{a : "s"}, 1, "s"], obj : {x : "s", y : true} ) |
| 53 | + |
| 54 | + directiveOnField @skip(if:true) |
| 55 | + } |
| 56 | + |
| 57 | + fragment comparisonFields on Character { |
| 58 | + name |
| 59 | + appearsIn |
| 60 | + friends { |
| 61 | + name |
| 62 | + } |
| 63 | + } |
| 64 | + ''' |
| 65 | + |
| 66 | + def "#1071 basic document serialisation"() { |
| 67 | + |
| 68 | + // this helps prove that we can serialise a query document |
| 69 | + // and hence allow caching of that parsed document via |
| 70 | + // systems that require serializable objects |
| 71 | + |
| 72 | + when: |
| 73 | + Document originalDoc = TestUtil.parseQuery(query) |
| 74 | + def originalAst = AstPrinter.printAst(originalDoc) |
| 75 | + |
| 76 | + Document newDocument = serialisedDownAndBack(originalDoc) |
| 77 | + def newAst = AstPrinter.printAst(newDocument) |
| 78 | + |
| 79 | + then: |
| 80 | + |
| 81 | + originalAst == newAst |
| 82 | + } |
| 83 | + |
| 84 | + def "basic error serialisation"() { |
| 85 | + when: |
| 86 | + GraphQLError syntaxError = new InvalidSyntaxError(srcLoc(1, 2), "Bad Syntax") |
| 87 | + |
| 88 | + GraphQLError serialisedError = serialisedDownAndBack(syntaxError) |
| 89 | + |
| 90 | + then: |
| 91 | + |
| 92 | + syntaxError.getMessage() == serialisedError.getMessage() |
| 93 | + syntaxError.getLocations() == serialisedError.getLocations() |
| 94 | + } |
| 95 | + |
| 96 | + def "PreparsedDocumentEntry with document is serializable"() { |
| 97 | + |
| 98 | + when: |
| 99 | + Document originalDoc = TestUtil.parseQuery(query) |
| 100 | + def originalEntry = new PreparsedDocumentEntry(originalDoc) |
| 101 | + def originalAst = AstPrinter.printAst(originalEntry.getDocument()) |
| 102 | + |
| 103 | + PreparsedDocumentEntry newEntry = serialisedDownAndBack(originalEntry) |
| 104 | + def newAst = AstPrinter.printAst(newEntry.getDocument()) |
| 105 | + |
| 106 | + then: |
| 107 | + |
| 108 | + originalAst == newAst |
| 109 | + } |
| 110 | + |
| 111 | + def "PreparsedDocumentEntry with errors is serializable"() { |
| 112 | + |
| 113 | + when: |
| 114 | + GraphQLError syntaxError1 = new InvalidSyntaxError(srcLoc(1, 1), "Bad Syntax 1") |
| 115 | + GraphQLError validationError2 = new ValidationError(ValidationErrorType.FieldUndefined, srcLoc(2, 2), "Bad Query 2") |
| 116 | + def originalEntry = new PreparsedDocumentEntry([syntaxError1, validationError2]) |
| 117 | + |
| 118 | + PreparsedDocumentEntry newEntry = serialisedDownAndBack(originalEntry) |
| 119 | + |
| 120 | + then: |
| 121 | + |
| 122 | + newEntry.getErrors().size() == 2 |
| 123 | + newEntry.getErrors().get(0).getMessage() == syntaxError1.getMessage() |
| 124 | + newEntry.getErrors().get(0).getLocations() == syntaxError1.getLocations() |
| 125 | + |
| 126 | + newEntry.getErrors().get(1).getMessage() == validationError2.getMessage() |
| 127 | + newEntry.getErrors().get(1).getLocations() == validationError2.getLocations() |
| 128 | + } |
| 129 | +} |
0 commit comments