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 @namespaced directive for namespacing by seperation of concerns #2478

Merged
merged 9 commits into from
Dec 6, 2023

Conversation

spawnia
Copy link
Collaborator

@spawnia spawnia commented Nov 28, 2023

Resolves #2266, supersedes #2469

  • Added or updated tests
  • Documented user facing changes
  • Updated CHANGELOG.md

Changes

Adds the @namespaced directive.

Breaking changes

None.

# Conflicts:
#	docs/master/api-reference/directives.md
#	src/Schema/Directives/NamespacedDirective.php
@spawnia
Copy link
Collaborator Author

spawnia commented Nov 28, 2023

@sniper7kills Could you rebase your pull request atop this branch and add a test? I think directives such as @find or @create might not work as intended in the example.

@spawnia spawnia added the enhancement A feature or improvement label Nov 28, 2023
@sniper7kills
Copy link
Contributor

@spawnia If you could give me a little push-start I'd be more than glad to take a shot at implementing some tests for this.
(Although I'm likely not the best candidate to be doing such, I'll give it a go)

I'm in the namespaced-directive branch with the following test; but I get an error "No directive found for namespaced". If you could give me a base test to start off of I'd be grateful.

    public function testNamespaced(): void
    {

        $this->mockResolver()
            ->with(null, [
                'foo' => 1,
            ]);

        $this->schema = /** @lang GraphQL */ 'type Query {
            foo: FooQueries! @namespaced
        }

        type FooQueries {
            single(id: Int @spread): Int
        }
        ';

        $this->graphQL(/** @lang GraphQL */ '{
            foo {
                single(id: 1)
            }
        }
        ');
    }

Regarding the @find and @create directives; I would love to learn more as to why you believe that. (I'm very new to this project and GraphQL as a whole) I have the following schema in my project and it appears to be working without issues, so if I have a better understanding I can try to build tests cases accordingly.

type Contact @model(class: "\\App\\Models\\Core\\Contact"){
    id: ID!

    company_id: ID

    name: String!

    "The contact profile photo URL"
    photo: String

    type: ContactTypes!

    description: String

    phone: String

    email: String

    company: Contact @BelongsTo

    employees: [Contact!] @HasMany

    "The notes for the contact"
    notes: [Note]! @morphMany

    "The attachments for the contact"
    attachments: [Attachment]! @morphMany

    "When the contact was created."
    created_at: DateTime!

    "When the contact was last updated."
    updated_at: DateTime!
}

enum ContactTypes {
    company
    person
}

extend type CoreQueries {
  contacts: ContactQueries! @namespaced
}

type ContactQueries {
  "Find a contact by an identifying attribute."
    single(
      "Search by primary key."
      id: ID @eq @rules(apply: ["prohibits:name", "required_without:name"])

      "Search by name."
      name: String @eq @rules(apply: ["prohibits:id", "required_without:id"])
    ): Contact @find

    "List multiple contacts."
    list(
      "Filters by name. Accepts SQL LIKE wildcards `%` and `_`."
      name: String @where(operator: "like")
      includeIds: [String!] @in(key: "id", , ignoreNull: true)
      parentId: String @where(key: "company_id", ignoreNull: true)
      type: ContactTypes @where(ignoreNull: true)
    ): [Contact!]! @paginate(defaultCount: 10)
}

extend type CoreMutations {
  contacts: ContactMutations! @namespaced
}

type ContactMutations {
  create(
    input: CreateContactInput! @spread
  ): Contact! @create
  update(
    input: UpdateContactInput @spread
  ): Contact! @update
  delete(
    id: String! @whereKey
  ): Contact @delete
}


input CreateContactInput {
    company_id: ID
    name: String!
    type: ContactTypes!
    company_id: ID
    description: String
    phone: String
    email: String

    file: VaporFileUpload

    notes: CreateNoteBelongsToMany
    attachments: CreateAttachmentBelongsToMany
}

input UpdateContactInput {
    id: ID!
    company_id: ID
    name: String
    type: ContactTypes
    description: String
    phone: String
    email: String

    file: VaporFileUpload

    notes: CreateNoteBelongsToMany
    attachments: CreateAttachmentBelongsToMany
}

input UpsertContactInput {
    id: ID
    company_id: ID
    name: String
    type: ContactTypes
    description: String
    phone: String
    email: String

    file: VaporFileUpload

    notes: CreateNoteBelongsToMany
    attachments: CreateAttachmentBelongsToMany
}

input CreateContactBelongsToMany{
    create: [CreateContactInput!]
    upsert: [UpsertContactInput!]
    connect: [ID!]
    sync: [ID!]
}

@spawnia
Copy link
Collaborator Author

spawnia commented Dec 5, 2023

Your test should look similar to the ones in https://github.com/nuwave/lighthouse/tree/master/tests/Integration/Schema/Directives. Make sure to extend the correct base class, then your directive should be found.

It is just because many directives are somewhat magical when it comes to figuring out the correct model to use, the correct namespace, etc.

@sniper7kills sniper7kills mentioned this pull request Dec 5, 2023
3 tasks
@sniper7kills
Copy link
Contributor

Ah! Thank you! I was mistakenly making a Unit Test instead of an Integration Test.

Please let me know if there are any special test cases you would like me to make to address any concerns I simply duplicated the nest directive test.

@spawnia spawnia marked this pull request as ready for review December 6, 2023 09:47
@spawnia spawnia merged commit 90950d1 into master Dec 6, 2023
@spawnia spawnia deleted the namespaced-directive branch December 6, 2023 09:50
@spawnia
Copy link
Collaborator Author

spawnia commented Dec 6, 2023

Thank you @sniper7kills, works as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Namespacing by separation of concerns
2 participants