Python graphene-graphql, are global constants for SQL queries a good idea?












2














For python 3.6.5 using graphene-graphql and mysql-connector



Context:
I wrote an API using graphene, a python graphQL implementation, this is actually my first time using the graphQL specification so I'm looking to improve my code implementation for the resolvers using a MySQL database.



As you may know, GraphQL isn't tied to any specific database or storage engine so it specifies a resolve method, where you "resolve" for a field in your Type. eg. User has a field name, so the resolve method would be how you get that name.



All my types are in a package: mytypes with modules for each type, and they all follow the same styling as the following code example.



Code:
Please note how the last two functions are different implementations I've tried, more on this on my concerns down below.



import graphene

from database.connection import request

from mytypes.user import User
from mytypes.format import Format

_STUDENT_ID = 'SELECT student_id FROM Rubrics WHERE id = %s'
_EVALUATOR_ID = 'SELECT evaluator_id FROM Rubrics WHERE id = %s'
_PROJECT_NAME = 'SELECT project_name FROM Rubrics WHERE id = %s'

class Rubric(graphene.ObjectType):
uid = graphene.ID()
evaluator = graphene.Field(User)
student = graphene.Field(User)
rubricFormat = graphene.Field(Format)
projectName = graphene.String()
evaluationDate = graphene.Date()
isPublic = graphene.Boolean()

def resolve_evaluator(self, info):
evaluator_id = request(_EVALUATOR_ID, self.uid)
return User(uid=evaluator_id)

def resolve_student(self, info):
student_id = request(_STUDENT_ID, self.uid)
return User(uid=student_id)

def resolve_rubricFormat(self, info):
request = 'SELECT format_id FROM Rubrics WHERE id = %s'
format_id = make(request, self.uid)
return Format(uid=format_id)

def resolve_isPublic(self, info):
return rubric.is_public(self.uid)


Concerns:




  • Readability


My first concern is about readability, I'm trying to follow the PEP 8 styling for Python code which states that I shouldn't use or should avoid




More than one space around an assignment (or other) operator to align it with another.




Which I do when defining my fields (uid, evaluator, student, etc), but this way it makes scanning it better in my opinion.




  • Resolve Implementation


As for the two methods I mentioned, first for resolve_rubricFormat I like the way make(request, self.uid) was read, where "make" was the same request imported as make but I realized I was just over-complicating.



Then, for resolve_isPublic I was delegating the data fetching to a package resolvers that had modules for every type and had all the logic there, but switching between both the Type and its resolvers/gets at the other package was tiring not to mention the original resolve methods would lose their significance since I was calling another method that was doing what the original resolve was intended for.




  • Global Variables


So I switched to global variables where I place the actual SQL query and use it as request(_WHATEVER, id) eg. request(_PROJECT_NAME, self.uid) would return the project name for the specified id, and since its on a module level its understood we are talking about the Rubric id.



But, I'm not sure if this is a good idea, what are your thoughts on this, any way to do it better?










share|improve this question









New contributor




Eber A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    2














    For python 3.6.5 using graphene-graphql and mysql-connector



    Context:
    I wrote an API using graphene, a python graphQL implementation, this is actually my first time using the graphQL specification so I'm looking to improve my code implementation for the resolvers using a MySQL database.



    As you may know, GraphQL isn't tied to any specific database or storage engine so it specifies a resolve method, where you "resolve" for a field in your Type. eg. User has a field name, so the resolve method would be how you get that name.



    All my types are in a package: mytypes with modules for each type, and they all follow the same styling as the following code example.



    Code:
    Please note how the last two functions are different implementations I've tried, more on this on my concerns down below.



    import graphene

    from database.connection import request

    from mytypes.user import User
    from mytypes.format import Format

    _STUDENT_ID = 'SELECT student_id FROM Rubrics WHERE id = %s'
    _EVALUATOR_ID = 'SELECT evaluator_id FROM Rubrics WHERE id = %s'
    _PROJECT_NAME = 'SELECT project_name FROM Rubrics WHERE id = %s'

    class Rubric(graphene.ObjectType):
    uid = graphene.ID()
    evaluator = graphene.Field(User)
    student = graphene.Field(User)
    rubricFormat = graphene.Field(Format)
    projectName = graphene.String()
    evaluationDate = graphene.Date()
    isPublic = graphene.Boolean()

    def resolve_evaluator(self, info):
    evaluator_id = request(_EVALUATOR_ID, self.uid)
    return User(uid=evaluator_id)

    def resolve_student(self, info):
    student_id = request(_STUDENT_ID, self.uid)
    return User(uid=student_id)

    def resolve_rubricFormat(self, info):
    request = 'SELECT format_id FROM Rubrics WHERE id = %s'
    format_id = make(request, self.uid)
    return Format(uid=format_id)

    def resolve_isPublic(self, info):
    return rubric.is_public(self.uid)


    Concerns:




    • Readability


    My first concern is about readability, I'm trying to follow the PEP 8 styling for Python code which states that I shouldn't use or should avoid




    More than one space around an assignment (or other) operator to align it with another.




    Which I do when defining my fields (uid, evaluator, student, etc), but this way it makes scanning it better in my opinion.




    • Resolve Implementation


    As for the two methods I mentioned, first for resolve_rubricFormat I like the way make(request, self.uid) was read, where "make" was the same request imported as make but I realized I was just over-complicating.



    Then, for resolve_isPublic I was delegating the data fetching to a package resolvers that had modules for every type and had all the logic there, but switching between both the Type and its resolvers/gets at the other package was tiring not to mention the original resolve methods would lose their significance since I was calling another method that was doing what the original resolve was intended for.




    • Global Variables


    So I switched to global variables where I place the actual SQL query and use it as request(_WHATEVER, id) eg. request(_PROJECT_NAME, self.uid) would return the project name for the specified id, and since its on a module level its understood we are talking about the Rubric id.



    But, I'm not sure if this is a good idea, what are your thoughts on this, any way to do it better?










    share|improve this question









    New contributor




    Eber A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      2












      2








      2


      1





      For python 3.6.5 using graphene-graphql and mysql-connector



      Context:
      I wrote an API using graphene, a python graphQL implementation, this is actually my first time using the graphQL specification so I'm looking to improve my code implementation for the resolvers using a MySQL database.



      As you may know, GraphQL isn't tied to any specific database or storage engine so it specifies a resolve method, where you "resolve" for a field in your Type. eg. User has a field name, so the resolve method would be how you get that name.



      All my types are in a package: mytypes with modules for each type, and they all follow the same styling as the following code example.



      Code:
      Please note how the last two functions are different implementations I've tried, more on this on my concerns down below.



      import graphene

      from database.connection import request

      from mytypes.user import User
      from mytypes.format import Format

      _STUDENT_ID = 'SELECT student_id FROM Rubrics WHERE id = %s'
      _EVALUATOR_ID = 'SELECT evaluator_id FROM Rubrics WHERE id = %s'
      _PROJECT_NAME = 'SELECT project_name FROM Rubrics WHERE id = %s'

      class Rubric(graphene.ObjectType):
      uid = graphene.ID()
      evaluator = graphene.Field(User)
      student = graphene.Field(User)
      rubricFormat = graphene.Field(Format)
      projectName = graphene.String()
      evaluationDate = graphene.Date()
      isPublic = graphene.Boolean()

      def resolve_evaluator(self, info):
      evaluator_id = request(_EVALUATOR_ID, self.uid)
      return User(uid=evaluator_id)

      def resolve_student(self, info):
      student_id = request(_STUDENT_ID, self.uid)
      return User(uid=student_id)

      def resolve_rubricFormat(self, info):
      request = 'SELECT format_id FROM Rubrics WHERE id = %s'
      format_id = make(request, self.uid)
      return Format(uid=format_id)

      def resolve_isPublic(self, info):
      return rubric.is_public(self.uid)


      Concerns:




      • Readability


      My first concern is about readability, I'm trying to follow the PEP 8 styling for Python code which states that I shouldn't use or should avoid




      More than one space around an assignment (or other) operator to align it with another.




      Which I do when defining my fields (uid, evaluator, student, etc), but this way it makes scanning it better in my opinion.




      • Resolve Implementation


      As for the two methods I mentioned, first for resolve_rubricFormat I like the way make(request, self.uid) was read, where "make" was the same request imported as make but I realized I was just over-complicating.



      Then, for resolve_isPublic I was delegating the data fetching to a package resolvers that had modules for every type and had all the logic there, but switching between both the Type and its resolvers/gets at the other package was tiring not to mention the original resolve methods would lose their significance since I was calling another method that was doing what the original resolve was intended for.




      • Global Variables


      So I switched to global variables where I place the actual SQL query and use it as request(_WHATEVER, id) eg. request(_PROJECT_NAME, self.uid) would return the project name for the specified id, and since its on a module level its understood we are talking about the Rubric id.



      But, I'm not sure if this is a good idea, what are your thoughts on this, any way to do it better?










      share|improve this question









      New contributor




      Eber A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      For python 3.6.5 using graphene-graphql and mysql-connector



      Context:
      I wrote an API using graphene, a python graphQL implementation, this is actually my first time using the graphQL specification so I'm looking to improve my code implementation for the resolvers using a MySQL database.



      As you may know, GraphQL isn't tied to any specific database or storage engine so it specifies a resolve method, where you "resolve" for a field in your Type. eg. User has a field name, so the resolve method would be how you get that name.



      All my types are in a package: mytypes with modules for each type, and they all follow the same styling as the following code example.



      Code:
      Please note how the last two functions are different implementations I've tried, more on this on my concerns down below.



      import graphene

      from database.connection import request

      from mytypes.user import User
      from mytypes.format import Format

      _STUDENT_ID = 'SELECT student_id FROM Rubrics WHERE id = %s'
      _EVALUATOR_ID = 'SELECT evaluator_id FROM Rubrics WHERE id = %s'
      _PROJECT_NAME = 'SELECT project_name FROM Rubrics WHERE id = %s'

      class Rubric(graphene.ObjectType):
      uid = graphene.ID()
      evaluator = graphene.Field(User)
      student = graphene.Field(User)
      rubricFormat = graphene.Field(Format)
      projectName = graphene.String()
      evaluationDate = graphene.Date()
      isPublic = graphene.Boolean()

      def resolve_evaluator(self, info):
      evaluator_id = request(_EVALUATOR_ID, self.uid)
      return User(uid=evaluator_id)

      def resolve_student(self, info):
      student_id = request(_STUDENT_ID, self.uid)
      return User(uid=student_id)

      def resolve_rubricFormat(self, info):
      request = 'SELECT format_id FROM Rubrics WHERE id = %s'
      format_id = make(request, self.uid)
      return Format(uid=format_id)

      def resolve_isPublic(self, info):
      return rubric.is_public(self.uid)


      Concerns:




      • Readability


      My first concern is about readability, I'm trying to follow the PEP 8 styling for Python code which states that I shouldn't use or should avoid




      More than one space around an assignment (or other) operator to align it with another.




      Which I do when defining my fields (uid, evaluator, student, etc), but this way it makes scanning it better in my opinion.




      • Resolve Implementation


      As for the two methods I mentioned, first for resolve_rubricFormat I like the way make(request, self.uid) was read, where "make" was the same request imported as make but I realized I was just over-complicating.



      Then, for resolve_isPublic I was delegating the data fetching to a package resolvers that had modules for every type and had all the logic there, but switching between both the Type and its resolvers/gets at the other package was tiring not to mention the original resolve methods would lose their significance since I was calling another method that was doing what the original resolve was intended for.




      • Global Variables


      So I switched to global variables where I place the actual SQL query and use it as request(_WHATEVER, id) eg. request(_PROJECT_NAME, self.uid) would return the project name for the specified id, and since its on a module level its understood we are talking about the Rubric id.



      But, I'm not sure if this is a good idea, what are your thoughts on this, any way to do it better?







      python mysql






      share|improve this question









      New contributor




      Eber A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Eber A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 hours ago





















      New contributor




      Eber A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      Eber A.

      112




      112




      New contributor




      Eber A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Eber A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Eber A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Eber A. is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210623%2fpython-graphene-graphql-are-global-constants-for-sql-queries-a-good-idea%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Eber A. is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Eber A. is a new contributor. Be nice, and check out our Code of Conduct.













          Eber A. is a new contributor. Be nice, and check out our Code of Conduct.












          Eber A. is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210623%2fpython-graphene-graphql-are-global-constants-for-sql-queries-a-good-idea%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre