Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @import directive #228

Merged
merged 4 commits into from
Jan 8, 2024
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
14 changes: 14 additions & 0 deletions Tests/ApolloCodegenInternalTestHelpers/MockCompilationResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,17 @@ public extension CompilationResult.Directive {
)
}
}

public extension CompilationResult.Argument {
static func mock(
_ name: String,
value: GraphQLValue
) -> Self {
Self(
name: name,
type: .nonNull(.string()),
value: value,
deprecationReason: nil
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
import XCTest
import Nimble
import GraphQLCompiler
import ApolloInternalTestHelpers
import ApolloCodegenInternalTestHelpers
@testable import ApolloCodegenLib

class CompilationApolloSpecificDirectiveTests: XCTestCase {

var schemaSDL: String!
var schemaJSON: String!
var document: String!

override func setUpWithError() throws {
try super.setUpWithError()

}

override func tearDown() {
schemaSDL = nil
schemaJSON = nil
document = nil

super.tearDown()
}

// MARK: - Helpers

func compileFrontend(
schemaNamespace: String = "TestSchema"
) async throws -> CompilationResult {
let frontend = try await GraphQLJSFrontend()
let config = ApolloCodegen.ConfigurationContext(config: .mock(schemaNamespace: schemaNamespace))

if let schemaSDL = schemaSDL {
return try await frontend.compile(
schema: schemaSDL,
document: document,
config: config
)
} else if let schemaJSON = schemaJSON {
return try await frontend.compile(
schemaJSON: schemaJSON,
document: document,
config: config
)
} else {
throw TestError("No Schema!")
}
}

func useStarWarsSchema() throws {
schemaJSON = try String(
contentsOf: ApolloCodegenInternalTestHelpers.Resources.StarWars.JSONSchema
)
}

// MARK: @apollo_client_ios_localCacheMutation Tests

/// Tests that we automatically add the local cache mutation directive to the schema
/// during codegen.
func test__compile__givenSchemaSDL_queryWithLocalCacheMutationDirective_notInSchema_hasDirective() async throws {
schemaSDL = """
type Query {
allAnimals: [Animal!]
}

interface Animal {
species: String!
}
"""

document = """
query Test @apollo_client_ios_localCacheMutation {
allAnimals {
species
}
}
"""

let expectedDirectives: [CompilationResult.Directive] = [
.mock("apollo_client_ios_localCacheMutation")
]

let compilationResult = try await compileFrontend()


let operation = try XCTUnwrap(compilationResult.operations.first)
expect(operation.directives).to(equal(expectedDirectives))
}

/// Tests that we automatically add the local cache mutation directive to the schema
/// during codegen.
func test__compile__givenSchemaJSON_queryWithLocalCacheMutationDirective_notInSchema_hasDirective() async throws {
try useStarWarsSchema()

document = """
query HeroAndFriendsNames($id: ID) @apollo_client_ios_localCacheMutation {
human(id: $id) {
name
mass
appearsIn
}
}
"""

let expectedDirectives: [CompilationResult.Directive] = [
.mock("apollo_client_ios_localCacheMutation")
]

let compilationResult = try await compileFrontend()


let operation = try XCTUnwrap(compilationResult.operations.first)
expect(operation.directives).to(equal(expectedDirectives))
}

func test__compile__givenQueryWithLocalCacheMutationDirective_stripsDirectiveFromSource() async throws {
schemaSDL = """
type Query {
allAnimals: [Animal!]
}

interface Animal {
species: String!
}
"""

document = """
query Test @apollo_client_ios_localCacheMutation {
allAnimals {
species
}
}
"""

let expectedDirectives: [CompilationResult.Directive] = [
.mock("import", arguments: [.mock("module", value: .string("MyModuleName"))])
]

let compilationResult = try await compileFrontend()


let operation = try XCTUnwrap(compilationResult.operations.first)
expect(operation.source).toNot(contain("@apollo_client_ios_localCacheMutation"))
}

// MARK: @import Tests

/// Tests that we automatically add the import directive to the schema
/// during codegen.
func test__compile__givenSchemaSDL_queryWithImportDirective_notInSchema_hasDirective() async throws {
schemaSDL = """
type Query {
allAnimals: [Animal!]
}

interface Animal {
species: String!
}
"""

document = """
query Test @import(module: "MyModuleName") {
allAnimals {
species
}
}
"""

let expectedDirectives: [CompilationResult.Directive] = [
.mock("import", arguments: [.mock("module", value: .string("MyModuleName"))])
]

let compilationResult = try await compileFrontend()


let operation = try XCTUnwrap(compilationResult.operations.first)
expect(operation.directives).to(equal(expectedDirectives))
}

/// Tests that we automatically add the import directive to the schema
/// during codegen.
func test__compile__givenSchemaJSON_queryWithImportDirective_notInSchema_hasDirective() async throws {
try useStarWarsSchema()

document = """
query HeroAndFriendsNames($id: ID) @import(module: "MyModuleName") {
human(id: $id) {
name
mass
appearsIn
}
}
"""

let expectedDirectives: [CompilationResult.Directive] = [
.mock("import", arguments: [.mock("module", value: .string("MyModuleName"))])
]

let compilationResult = try await compileFrontend()


let operation = try XCTUnwrap(compilationResult.operations.first)
expect(operation.directives).to(equal(expectedDirectives))
}

func test__compile__givenQueryWithImportDirective_stripsDirectiveFromSource() async throws {
schemaSDL = """
type Query {
allAnimals: [Animal!]
}

interface Animal {
species: String!
}
"""

document = """
query Test @import(module: "MyModuleName") {
allAnimals {
species
}
}
"""

let expectedDirectives: [CompilationResult.Directive] = [
.mock("import", arguments: [.mock("module", value: .string("MyModuleName"))])
]

let compilationResult = try await compileFrontend()


let operation = try XCTUnwrap(compilationResult.operations.first)
expect(operation.source).toNot(contain("@import"))
}

}
58 changes: 0 additions & 58 deletions Tests/ApolloCodegenTests/Frontend/CompilationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,64 +130,6 @@ class CompilationTests: XCTestCase {
expect(operation.directives).to(equal(expectedDirectives))
}

/// Tests that we automatically add the local cache mutation directive to the schema
/// during codegen.
func test__compile__givenSchemaSDL_queryWithLocalCacheMutationDirective_notInSchema_hasDirective() async throws {
schemaSDL = """
type Query {
allAnimals: [Animal!]
}

interface Animal {
species: String!
}
"""

document = """
query Test @apollo_client_ios_localCacheMutation {
allAnimals {
species
}
}
"""

let expectedDirectives: [CompilationResult.Directive] = [
.mock("apollo_client_ios_localCacheMutation")
]

let compilationResult = try await compileFrontend()


let operation = try XCTUnwrap(compilationResult.operations.first)
expect(operation.directives).to(equal(expectedDirectives))
}

/// Tests that we automatically add the local cache mutation directive to the schema
/// during codegen.
func test__compile__givenSchemaJSON_queryWithLocalCacheMutationDirective_notInSchema_hasDirective() async throws {
try useStarWarsSchema()

document = """
query HeroAndFriendsNames($id: ID) @apollo_client_ios_localCacheMutation {
human(id: $id) {
name
mass
appearsIn
}
}
"""

let expectedDirectives: [CompilationResult.Directive] = [
.mock("apollo_client_ios_localCacheMutation")
]

let compilationResult = try await compileFrontend()


let operation = try XCTUnwrap(compilationResult.operations.first)
expect(operation.directives).to(equal(expectedDirectives))
}

func test__compile__givenInputObject_withListFieldWithDefaultValueEmptyArray() async throws {
// given
schemaSDL = """
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@types/jest": "29.5.11",
"common-tags": "1.8.2",
"jest": "29.7.0",
"rollup": "4.9.2",
"rollup": "4.9.4",
"@rollup/plugin-terser": "0.4.4",
"ts-jest": "29.1.1",
"typescript": "5.3.3"
Expand Down
Loading
Loading